Sort Colors
Function Name: sortColors
Description
Given an array consisting of 'n' elements that represent colors as red, white, or blue, sort the array in-place so that elements of the same color are adjacent, with the colors in the order of red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. You must solve this problem with a one-pass algorithm using only constant space.
Requirements
- The function 'sortColors' should take a single parameter: an array of integers where each integer is either 0, 1, or 2.
- The function should sort the array in-place, meaning no new array should be created for the sorting process.
- The function must have an O(n) complexity, where 'n' is the number of elements in the array, thus ensuring a single pass over the input array.
- No library functions for sorting should be used; the sorting logic must be implemented manually.
- The function should not return anything; it should modify the input array directly.
Examples
Given input: [2, 0, 1]After calling sortColors(input), the input array should be modified to: [0, 1, 2]
Links
Merge Intervals
Tue Oct 29 2024
Write a function 'mergeIntervals' that takes an array of intervals where each interval is a pair of integers representing an inclusive range. The function should merge all overlapping intervals and return an array of the merged intervals. An interval [a, b] is said to overlap with another interval [c, d] if they have at least one number in common. The resulting intervals should be outputted in ascending order, sorted by their start values.The function should handle an empty array, and if no intervals overlap, it should return the intervals as they were.
Matrix Spiral Copy
Thu Oct 31 2024
Write a function that takes a 2D array (matrix) of m x n elements and returns a list of its elements sorted in a spiral order.The spiral order starts at the top-left corner of the matrix and proceeds in a clockwise direction.The function should move towards the right on the top row, then proceed downwards along the last column, then move leftward along the bottom row, and finally move up the first column, then continue inwards, following the same pattern until all elements have been visited.