Merge Intervals
Function Name: mergeIntervals
Description
Given a collection of intervals, each represented as a pair of integers, write a function 'mergeIntervals' that merges all overlapping intervals. The function should return an array of intervals where overlapping intervals are merged into single intervals that span from the smallest to the largest numbers included in any of the overlapping intervals.
Intervals are represented as an array/pair of two integers, for example, [1, 3]. An interval [a, b] is considered to overlap with [c, d] if there's any number x such that a <= x <= b and c <= x <= d.
The returned intervals should be in ascending order of their start values. Assume that the input list is unsorted.
Requirements
- The function should take a single parameter - an array of interval pairs.
- The function should return a sorted array of non-overlapping intervals.
- If the intervals overlap, merge them into a single interval.
- Intervals will be represented as arrays with two integers [start, end].
- All the integers in the intervals will be positive integers.
- The function should handle an empty array input and return an empty array.
Examples
Function Call: mergeIntervals([[1,3], [2,6], [8,10], [15,18]])Output: [[1,6], [8,10], [15,18]]Explanation: Since intervals [1,3] and [2,6] overlap, they are merged into [1,6].Function Call: mergeIntervals([[6,8], [1,9], [2,4], [4,7]])Output: [[1,9]]Explanation: All the provided intervals overlap, resulting in a single merged interval [1,9].
Links
CircularQueue
Sun Oct 06 2024
In this challenge, you are tasked with implementing a Circular Queue data structure. The circular queue is a linear data structure that follows the First In First Out (FIFO) principle but also connects the last position back to the first position to make a circle. It is ideal for scenarios where the queue needs to be reset after it reaches the end.Your implementation should support the following operations: enqueue, which inserts an element at the rear of the queue; dequeue, which removes and returns the element from the front of the queue; peek, which returns the front-most element without removing it from the queue; and isEmpty, which checks whether the queue is empty.
Unique Paths in a Grid
Tue Oct 08 2024
Write a function that calculates the number of unique paths from the top-left corner to the bottom-right corner of a m x n grid. You are only allowed to move either down or right at any point in time.The function should take two parameters which are the dimensions of the grid (m for rows and n for columns).Assume both m and n will be greater than or equal to 1.