Unique Paths in a Grid
Function Name: countUniquePaths
Description
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.
Requirements
- The function must be recursive.
- No use of global or static variables.
- No side-effects (e.g., modifying input parameters).
- Optimize the function to handle larger grids efficiently.
Examples
If the grid is 2x2, there are two unique paths (right-down or down-right).countUniquePaths(2, 2) should return 2.For a grid of 3x3, the function should return 6.
Links
Merge Intervals
Mon Oct 07 2024
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.
Rainwater Trapping
Wed Oct 09 2024
Write a function called trapRainwater that calculates how much rainwater can be trapped between the given heights of bars. You are given an array that represents an elevation map where the width of each bar is 1. The function should compute how much water is trapped after raining. Bars of width 1 and non-negative integers are represented in the array where each value represents the height of the bar at that position.