Skip to content

Commit 681d8bf

Browse files
committed
Added tasks 39-55
1 parent 1b255ad commit 681d8bf

File tree

11 files changed

+723
-0
lines changed

11 files changed

+723
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7979
|-|-|-|-|-|-
80+
| 0046 |[Permutations](src/main/js/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
8081

8182
#### Day 12 Dynamic Programming
8283

@@ -148,6 +149,7 @@
148149

149150
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
150151
|-|-|-|-|-|-
152+
| 0039 |[Combination Sum](src/main/js/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 1 | 98.51
151153

152154
#### Day 11 Recursion Backtracking
153155

@@ -160,11 +162,13 @@
160162

161163
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
162164
|-|-|-|-|-|-
165+
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
163166

164167
#### Day 13 Dynamic Programming
165168

166169
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
167170
|-|-|-|-|-|-
171+
| 0045 |[Jump Game II](src/main/js/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
168172

169173
#### Day 14 Dynamic Programming
170174

@@ -395,11 +399,14 @@
395399

396400
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
397401
|-|-|-|-|-|-
402+
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
403+
| 0045 |[Jump Game II](src/main/js/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
398404

399405
#### Day 5
400406

401407
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
402408
|-|-|-|-|-|-
409+
| 0053 |[Maximum Subarray](src/main/js/g0001_0100/s0053_maximum_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 1 | 85.69
403410

404411
#### Day 6
405412

@@ -420,6 +427,7 @@
420427

421428
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
422429
|-|-|-|-|-|-
430+
| 0042 |[Trapping Rain Water](src/main/js/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
423431

424432
#### Day 10
425433

@@ -580,6 +588,7 @@
580588

581589
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
582590
|-|-|-|-|-|-
591+
| 0048 |[Rotate Image](src/main/js/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
583592

584593
#### Day 8
585594

@@ -600,6 +609,7 @@
600609

601610
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
602611
|-|-|-|-|-|-
612+
| 0049 |[Group Anagrams](src/main/js/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 24 | 91.62
603613

604614
#### Day 12
605615

@@ -954,6 +964,8 @@
954964

955965
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
956966
|-|-|-|-|-|-
967+
| 0039 |[Combination Sum](src/main/js/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 1 | 98.51
968+
| 0046 |[Permutations](src/main/js/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
957969

958970
### Udemy
959971

@@ -971,6 +983,7 @@
971983
| 0003 |[Longest Substring Without Repeating Characters](src/main/js/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 3 | 98.96
972984
| 0020 |[Valid Parentheses](src/main/js/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
973985
| 0005 |[Longest Palindromic Substring](src/main/js/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 10 | 99.54
986+
| 0049 |[Group Anagrams](src/main/js/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 24 | 91.62
974987

975988
#### Udemy Binary Search
976989

@@ -983,17 +996,21 @@
983996
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
984997
|-|-|-|-|-|-
985998
| 0001 |[Two Sum](src/main/js/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.15
999+
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1000+
| 0041 |[First Missing Positive](src/main/js/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 1 | 98.86
9861001

9871002
#### Udemy Two Pointers
9881003

9891004
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9901005
|-|-|-|-|-|-
1006+
| 0042 |[Trapping Rain Water](src/main/js/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
9911007
| 0015 |[3Sum](src/main/js/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 31 | 91.92
9921008

9931009
#### Udemy Famous Algorithm
9941010

9951011
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9961012
|-|-|-|-|-|-
1013+
| 0053 |[Maximum Subarray](src/main/js/g0001_0100/s0053_maximum_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 1 | 85.69
9971014

9981015
#### Udemy Sorting Algorithms
9991016

@@ -1004,6 +1021,7 @@
10041021

10051022
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10061023
|-|-|-|-|-|-
1024+
| 0048 |[Rotate Image](src/main/js/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
10071025

10081026
#### Udemy Linked List
10091027

@@ -1039,7 +1057,9 @@
10391057
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10401058
|-|-|-|-|-|-
10411059
| 0022 |[Generate Parentheses](src/main/js/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
1060+
| 0039 |[Combination Sum](src/main/js/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 1 | 98.51
10421061
| 0017 |[Letter Combinations of a Phone Number](src/main/js/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
1062+
| 0046 |[Permutations](src/main/js/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
10431063

10441064
#### Udemy Bit Manipulation
10451065

@@ -1057,6 +1077,7 @@
10571077

10581078
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10591079
|-|-|-|-|-|-
1080+
| 0053 |[Maximum Subarray](src/main/js/g0001_0100/s0053_maximum_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 1 | 85.69
10601081

10611082
#### Day 2 Array
10621083

@@ -1143,6 +1164,7 @@
11431164

11441165
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11451166
|-|-|-|-|-|-
1167+
| 0048 |[Rotate Image](src/main/js/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
11461168

11471169
#### Day 4 Array
11481170

@@ -1168,6 +1190,7 @@
11681190

11691191
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11701192
|-|-|-|-|-|-
1193+
| 0049 |[Group Anagrams](src/main/js/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 24 | 91.62
11711194

11721195
#### Day 9 String
11731196

@@ -1242,6 +1265,16 @@
12421265

12431266
| # | Title | Difficulty | Tag | Time, ms | Time, %
12441267
|------|----------------|-------------|-------------|----------|--------
1268+
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1269+
| 0053 |[Maximum Subarray](src/main/js/g0001_0100/s0053_maximum_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 1 | 85.69
1270+
| 0051 |[N-Queens](src/main/js/g0001_0100/s0051_n_queens)| Hard | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(N!)_Space_O(N) | 6 | 96.66
1271+
| 0049 |[Group Anagrams](src/main/js/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 24 | 91.62
1272+
| 0048 |[Rotate Image](src/main/js/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
1273+
| 0046 |[Permutations](src/main/js/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Algorithm_I_Day_11_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
1274+
| 0045 |[Jump Game II](src/main/js/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_4, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1275+
| 0042 |[Trapping Rain Water](src/main/js/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Dynamic_Programming_I_Day_9, Udemy_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1276+
| 0041 |[First Missing Positive](src/main/js/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 1 | 98.86
1277+
| 0039 |[Combination Sum](src/main/js/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n+2^n) | 1 | 98.51
12451278
| 0035 |[Search Insert Position](src/main/js/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
12461279
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/js/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
12471280
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/fork)
3+
4+
## 39\. Combination Sum
5+
6+
Medium
7+
8+
Given an array of **distinct** integers `candidates` and a target integer `target`, return _a list of all **unique combinations** of_ `candidates` _where the chosen numbers sum to_ `target`_._ You may return the combinations in **any order**.
9+
10+
The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
11+
12+
It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input.
13+
14+
**Example 1:**
15+
16+
**Input:** candidates = [2,3,6,7], target = 7
17+
18+
**Output:** [[2,2,3],[7]]
19+
20+
**Explanation:**
21+
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
22+
23+
7 is a candidate, and 7 = 7.
24+
25+
These are the only two combinations.
26+
27+
**Example 2:**
28+
29+
**Input:** candidates = [2,3,5], target = 8
30+
31+
**Output:** [[2,2,2,2],[2,3,3],[3,5]]
32+
33+
**Example 3:**
34+
35+
**Input:** candidates = [2], target = 1
36+
37+
**Output:** []
38+
39+
**Constraints:**
40+
41+
* `1 <= candidates.length <= 30`
42+
* `1 <= candidates[i] <= 200`
43+
* All elements of `candidates` are **distinct**.
44+
* `1 <= target <= 500`
45+
46+
## Solution
47+
48+
```javascript
49+
/**
50+
* @param {number[]} candidates
51+
* @param {number} target
52+
* @return {number[][]}
53+
*/
54+
var combinationSum = function(candidates, target) {
55+
const ans = []
56+
const subList = []
57+
58+
const combinationSumRec = (n, candidates, target, subList, ans) => {
59+
if (target === 0 || n === 0) {
60+
if (target === 0) {
61+
ans.push([...subList]) // Create a copy of subList
62+
}
63+
return
64+
}
65+
66+
if (target - candidates[n - 1] >= 0) {
67+
subList.push(candidates[n - 1])
68+
combinationSumRec(n, candidates, target - candidates[n - 1], subList, ans)
69+
subList.pop() // Backtracking step
70+
}
71+
72+
combinationSumRec(n - 1, candidates, target, subList, ans)
73+
}
74+
75+
combinationSumRec(candidates.length, candidates, target, subList, ans)
76+
return ans
77+
};
78+
79+
export { combinationSum }
80+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/fork)
3+
4+
## 41\. First Missing Positive
5+
6+
Hard
7+
8+
Given an unsorted integer array `nums`, return the smallest missing positive integer.
9+
10+
You must implement an algorithm that runs in `O(n)` time and uses constant extra space.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,0]
15+
16+
**Output:** 3
17+
18+
**Explanation:** The numbers in the range [1,2] are all in the array.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,4,-1,1]
23+
24+
**Output:** 2
25+
26+
**Explanation:** 1 is in the array but 2 is missing.
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [7,8,9,11,12]
31+
32+
**Output:** 1
33+
34+
**Explanation:** The smallest positive integer 1 is missing.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
39+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
40+
41+
## Solution
42+
43+
```javascript
44+
/**
45+
* @param {number[]} nums
46+
* @return {number}
47+
*/
48+
var firstMissingPositive = function(nums) {
49+
for (let i = 0; i < nums.length; i++) {
50+
while (
51+
nums[i] > 0 &&
52+
nums[i] <= nums.length &&
53+
nums[nums[i] - 1] !== nums[i]
54+
) {
55+
// Swap nums[i] with nums[nums[i] - 1]
56+
let temp = nums[nums[i] - 1]
57+
nums[nums[i] - 1] = nums[i]
58+
nums[i] = temp
59+
}
60+
}
61+
62+
for (let i = 0; i < nums.length; i++) {
63+
if (nums[i] !== i + 1) {
64+
return i + 1
65+
}
66+
}
67+
68+
return nums.length + 1
69+
};
70+
71+
export { firstMissingPositive }
72+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/fork)
3+
4+
## 42\. Trapping Rain Water
5+
6+
Hard
7+
8+
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)
13+
14+
**Input:** height = [0,1,0,2,1,0,1,3,2,1,2,1]
15+
16+
**Output:** 6
17+
18+
**Explanation:** The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
19+
20+
**Example 2:**
21+
22+
**Input:** height = [4,2,0,3,2,5]
23+
24+
**Output:** 9
25+
26+
**Constraints:**
27+
28+
* `n == height.length`
29+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
30+
* <code>0 <= height[i] <= 10<sup>5</sup></code>
31+
32+
## Solution
33+
34+
```javascript
35+
/**
36+
* @param {number[]} height
37+
* @return {number}
38+
*/
39+
var trap = function(height) {
40+
let l = 0
41+
let r = height.length - 1
42+
let res = 0
43+
let lowerWall = 0
44+
45+
while (l < r) {
46+
let lVal = height[l]
47+
let rVal = height[r]
48+
49+
// Determine the lower wall
50+
if (lVal < rVal) {
51+
// Update the lower wall based on the left pointer
52+
lowerWall = Math.max(lVal, lowerWall)
53+
// Add water trapped at the current position
54+
res += lowerWall - lVal
55+
// Move the left pointer
56+
l++
57+
} else {
58+
// Update the lower wall based on the right pointer
59+
lowerWall = Math.max(rVal, lowerWall)
60+
// Add water trapped at the current position
61+
res += lowerWall - rVal
62+
// Move the right pointer
63+
r--
64+
}
65+
}
66+
67+
return res
68+
};
69+
70+
export { trap }
71+
```

0 commit comments

Comments
 (0)