Manafall Daily Quest

Unique Paths in Grid

Function Name: countUniquePaths

Description

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.

Requirements

  • The function `countUniquePaths(m, n, obstacles)` should return an integer, the number of unique paths from top-left to bottom-right, excluding paths that encounter obstacles.
  • Consider the upper-left corner as starting point (0, 0) and the lower-right corner as end point (m-1, n-1).
  • If the starting cell or the ending cell has an obstacle, return 0.
  • The grid will always at least be a 1x1 grid.
  • The `obstacles` array will have the same dimensions as the grid (MxN), and only contain 0s and 1s, where 1 indicates an obstacle.

Examples

// Example 1:countUniquePaths(3, 3, [[0, 0, 0], [0, 1, 0], [0, 0, 0]]); // return 2// Example 2:countUniquePaths(1, 1, [[0]]); // return 1// Example 3:countUniquePaths(3, 3, [[1, 0, 0], [0, 1, 0], [0, 0, 0]]); // return 0

Links

https://en.wikipedia.org/wiki/Dynamic_programminghttps://leetcode.com/problems/unique-paths-ii/

Merge Intervals

Fri Oct 18 2024

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.

Prev Quest

Simple Change Counter

Sun Oct 20 2024

Create a function named 'calculateChange' that takes an amount of money (in cents) and returns the minimum number of coins that make up that amount. Assume that the available coins are quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent). The function should return an object/dictionary with the denomination of the coin as the key and the amount of each coin as the value.

Next Quest