Merge Intervals
Function Name: mergeIntervals
Description
Write a function called `mergeIntervals` that takes an array of intervals where each interval is represented as an array of two integers, denoting the inclusive start and the inclusive end of an interval. The function should merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input. The output array should be sorted by ascending starting points of the intervals.
Requirements
- The function should take a single parameter: an array of intervals, where each interval is also an array of two integers [start, end].
- Assume that the intervals can only contain integer values.
- If an interval is within another interval, it should also be merged.
- Intervals that are touching, meaning the end of one interval is the same as the start of another, should be merged.
- The input list of intervals is not necessarily ordered.
- The output should be an array of intervals sorted by ascending starting points, with all overlapping and touching intervals merged.
- The function should handle an empty list of intervals and return an empty list in this case.
Examples
Example 1:Input: 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].Example 2:Input: mergeIntervals([[1,4], [4,5]])Output: [[1,5]]Explanation: Intervals [1,4] and [4,5] are touching, so they are merged into [1,5].
Links
Counting Anagrams
Thu Oct 17 2024
Write a function `countAnagrams` that takes two parameters: a string `s` and a string `p`. The function should return the number of anagram substrings of `p` found in `s`. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, 'listen' and 'silent' are anagrams of each other.
Unique Paths in Grid
Sat Oct 19 2024
Write a function `countUniquePaths` that calculates the number of unique paths from the top-left corner to the bottom-right corner of a MxN grid. However, there are some 'obstacles' represented by a 1 placed in some cells. The function should take two parameters: `m` and `n` representing the dimensions of the grid, and `obstacles` a 2D array where `obstacles[i][j] == 1` means there's an obstacle on the (i, j) cell.The robot can only move either down or right at any point in time. If it encounters an obstacle, it cannot move further in that direction. If it's at the edge of the grid, it can only move in the direction that doesn't take it out of bounds.