Merge Intervals
Function Name: mergeIntervals
Description
Write a function 'mergeIntervals' that takes an array of intervals where each interval is represented as an array of two integers, and the intervals are sorted in ascending order based on the start of each interval. The function should merge all overlapping intervals and return an array of the resulting non-overlapping intervals sorted in ascending order.
Requirements
- The input must be an array of intervals, where each interval is also an array of two integers.
- The function must merge all overlapping intervals.
- The function should handle cases where no intervals overlap.
- If an interval is within another interval, the intervals should be merged.
- The original intervals must be sorted, but you may return the final intervals in any order.
- The function should not use any libraries or built-in sorting functions; implement any necessary sorting logic manually.
- The function must be able to handle an empty interval list.
- Optimize the function for time complexity.
Examples
mergeIntervals([[1, 4], [2, 5], [7, 9]]) should return [[1, 5], [7, 9]]mergeIntervals([[6, 7], [2, 4], [5, 9]]) should return [[2, 4], [5, 9]]mergeIntervals([[1, 4], [4, 5], [10, 15]]) should return [[1, 5], [10, 15]] because intervals [1,4] and [4,5] can be merged.mergeIntervals([]) should return []
Frequency Sort
Thu Dec 12 2024
Write a function named 'frequencySort' that sorts an array based on the frequency of the elements. If two elements have the same frequency, then the smaller number comes first.The function will take one parameter, an array of integers, and should return a new array of integers sorted based on the aforementioned criteria.
Uncommon Words from Two Sentences
Sat Dec 14 2024
Write a function named `findUncommonWords` that takes two strings `sentence1` and `sentence2`, which represent sentences composed of lowercase English letters and spaces (the spaces separate the words in the sentence).The function should return a list of the words that are uncommon in both sentences. A word is uncommon if it appears exactly once in one of the sentences and does not appear in the other sentence.The final list of uncommon words should not be in any particular order.