Minimal Swaps to Ascend
Function Name: minimalSwapsToSort
Description
Write a function that calculates the minimum number of swaps required to sort an array of unique integers in ascending order. The function should perform this calculation without actually sorting the array.
Each swap can only exchange two elements. Assume that the input array does not contain any duplicate values and contains at least two numbers.
Requirements
- The function must be named 'minimalSwapsToSort'.
- The function should take a single argument, which is an array of unique integers.
- The function should return a single integer representing the minimum number of swaps required to sort the array.
- You must not sort the array to solve this challenge.
- The function should handle arrays of varying sizes.
- Consider the time complexity of your solution; it should be efficient enough for large inputs.
Examples
Given the array [5, 1, 4, 2], 'minimalSwapsToSort' should return 3.Given the array [2, 4, 5, 1, 3], 'minimalSwapsToSort' should return 3.
Links
Aggregate Event Times
Sat Nov 09 2024
Write a function that takes a list of time intervals for various events, where each interval is represented as a pair of strings (start, end) in 24-hour 'HH:MM' format. The function should return a list of merged intervals if any two intervals overlap.The input list can be unsorted and there might be multiple intervals overlapping. The merged intervals should be outputted in sorted order, without any overlapping.Assume that time intervals are closed, meaning '10:00' - '12:00' and '12:00' - '13:00' do overlap at '12:00'.
Matrix Spiral Traversal
Mon Nov 11 2024
Write a function that takes a two-dimensional array (matrix) of MxN elements and returns an array of all elements in spiral order.The function should start at the top-left corner and proceed in a spiral pattern until every element has been visited, moving in the order of right, down, left, and up sides of the matrix's boundary.Consider that the input matrix can be of any size, but it is guaranteed to be non-empty.