diff --git a/challenges/533-trapping-rain-water/README.md b/challenges/533-trapping-rain-water/README.md new file mode 100644 index 0000000..b337a39 --- /dev/null +++ b/challenges/533-trapping-rain-water/README.md @@ -0,0 +1,35 @@ +Write a function `trap` that calculates how much rainwater can be trapped between bars in a histogram after raining. + +Given an array of non-negative integers `height` where each element represents the elevation at that point, compute how much water it can trap. + +### Signature + +```ts +function trap(height: number[]): number; +``` + +### Example + +```ts +trap([0,1,0,2,1,0,1,3,2,1,2,1]); // 6 +trap([4,2,0,3,2,5]); // 9 +``` + +### Explanation + +Each bar's trapped water is determined by the **minimum of the highest bars** to its left and right, minus its own height. + +For example, in `[0,1,0,2,1,0,1,3,2,1,2,1]`, at index 5 (`height = 0`): + +* Left max = 2 +* Right max = 3 +* Water trapped = `min(2, 3) - 0 = 2`. + +The total water trapped is the sum across all indices. + +### Constraints + +* `n == height.length` +* `0 ≤ n ≤ 10⁵` +* `0 ≤ height[i] ≤ 10⁴` +* The algorithm should run in `O(n)` time and `O(1)` extra space. diff --git a/challenges/533-trapping-rain-water/info.yml b/challenges/533-trapping-rain-water/info.yml new file mode 100644 index 0000000..cc82a25 --- /dev/null +++ b/challenges/533-trapping-rain-water/info.yml @@ -0,0 +1,11 @@ +difficulty: hard +title: Trapping Rain Water +type: question +template: javascript +tags: javascript, arrays, two-pointers +author: + github: jsartisan + name: Pawan Kumar + avatar_url: https://avatars.githubusercontent.com/u/6636360?v=4 +published_date: '2025-10-07' + diff --git a/challenges/533-trapping-rain-water/template.template.md b/challenges/533-trapping-rain-water/template.template.md new file mode 100644 index 0000000..9ee33bd --- /dev/null +++ b/challenges/533-trapping-rain-water/template.template.md @@ -0,0 +1,75 @@ +```js index.js +/** + * @param {number[]} height + * @return {number} + */ +export function trap(height) { + // TODO: Implement me + return 0; +} +``` + +```js index.test.js +import { trap } from './index'; + +describe('trap (JS)', () => { + it('handles empty array', () => { + expect(trap([])).toBe(0); + }); + + it('handles single bar', () => { + expect(trap([5])).toBe(0); + }); + + it('handles no trapped water', () => { + expect(trap([1,2,3,4,5])).toBe(0); + }); + + it('handles simple valley', () => { + expect(trap([2,0,2])).toBe(2); + }); + + it('handles complex case', () => { + expect(trap([0,1,0,2,1,0,1,3,2,1,2,1])).toBe(6); + }); + + it('handles another complex case', () => { + expect(trap([4,2,0,3,2,5])).toBe(9); + }); +}); +``` + +```ts index.ts +export function trap(height: number[]): number { + // TODO: Implement me + return 0; +} +``` + +```ts index.test.ts +import { trap } from './index'; + +describe('trap (TS)', () => { + it('returns 0 for empty array', () => { + expect(trap([])).toBe(0); + }); + + it('returns 0 for increasing heights', () => { + expect(trap([1,2,3,4])).toBe(0); + }); + + it('returns 2 for [2,0,2]', () => { + expect(trap([2,0,2])).toBe(2); + }); + + it('returns 6 for classic example', () => { + expect(trap([0,1,0,2,1,0,1,3,2,1,2,1])).toBe(6); + }); + + it('returns 9 for [4,2,0,3,2,5]', () => { + expect(trap([4,2,0,3,2,5])).toBe(9); + }); +}); +``` + +