Skip to content

Commit 69af8b5

Browse files
committed
Added tasks 39-55
1 parent 913ff11 commit 69af8b5

File tree

12 files changed

+690
-2
lines changed

12 files changed

+690
-2
lines changed

README.md

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

2424
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
2525
|-|-|-|-|-|-
26+
| 0053 |[Maximum Subarray](src/main/dart/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) | 361 | 88.89
2627

2728
#### Day 2 Array
2829

@@ -109,6 +110,7 @@
109110

110111
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
111112
|-|-|-|-|-|-
113+
| 0048 |[Rotate Image](src/main/dart/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) | 311 | 90.24
112114

113115
#### Day 4 Array
114116

@@ -134,6 +136,7 @@
134136

135137
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
136138
|-|-|-|-|-|-
139+
| 0049 |[Group Anagrams](src/main/dart/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) | 376 | 92.86
137140

138141
#### Day 9 String
139142

@@ -264,6 +267,7 @@
264267

265268
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
266269
|-|-|-|-|-|-
270+
| 0046 |[Permutations](src/main/dart/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 319 | 80.00
267271

268272
#### Day 12 Dynamic Programming
269273

@@ -335,6 +339,7 @@
335339

336340
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
337341
|-|-|-|-|-|-
342+
| 0039 |[Combination Sum](src/main/dart/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 316 | 96.88
338343

339344
#### Day 11 Recursion Backtracking
340345

@@ -347,11 +352,13 @@
347352

348353
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
349354
|-|-|-|-|-|-
355+
| 0055 |[Jump Game](src/main/dart/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) | 331 | 97.53
350356

351357
#### Day 13 Dynamic Programming
352358

353359
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
354360
|-|-|-|-|-|-
361+
| 0045 |[Jump Game II](src/main/dart/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 335 | 81.58
355362

356363
#### Day 14 Dynamic Programming
357364

@@ -582,11 +589,14 @@
582589

583590
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
584591
|-|-|-|-|-|-
592+
| 0055 |[Jump Game](src/main/dart/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) | 331 | 97.53
593+
| 0045 |[Jump Game II](src/main/dart/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 335 | 81.58
585594

586595
#### Day 5
587596

588597
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
589598
|-|-|-|-|-|-
599+
| 0053 |[Maximum Subarray](src/main/dart/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) | 361 | 88.89
590600

591601
#### Day 6
592602

@@ -607,6 +617,7 @@
607617

608618
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
609619
|-|-|-|-|-|-
620+
| 0042 |[Trapping Rain Water](src/main/dart/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) | 328 | 80.77
610621

611622
#### Day 10
612623

@@ -767,6 +778,7 @@
767778

768779
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
769780
|-|-|-|-|-|-
781+
| 0048 |[Rotate Image](src/main/dart/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) | 311 | 90.24
770782

771783
#### Day 8
772784

@@ -787,6 +799,7 @@
787799

788800
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
789801
|-|-|-|-|-|-
802+
| 0049 |[Group Anagrams](src/main/dart/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) | 376 | 92.86
790803

791804
#### Day 12
792805

@@ -1141,6 +1154,8 @@
11411154

11421155
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11431156
|-|-|-|-|-|-
1157+
| 0039 |[Combination Sum](src/main/dart/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 316 | 96.88
1158+
| 0046 |[Permutations](src/main/dart/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 319 | 80.00
11441159

11451160
### Udemy
11461161

@@ -1158,6 +1173,7 @@
11581173
| 0003 |[Longest Substring Without Repeating Characters](src/main/dart/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) | 336 | 99.68
11591174
| 0020 |[Valid Parentheses](src/main/dart/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 298 | 95.87
11601175
| 0005 |[Longest Palindromic Substring](src/main/dart/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) | 297 | 97.22
1176+
| 0049 |[Group Anagrams](src/main/dart/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) | 376 | 92.86
11611177

11621178
#### Udemy Binary Search
11631179

@@ -1170,17 +1186,21 @@
11701186
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11711187
|-|-|-|-|-|-
11721188
| 0001 |[Two Sum](src/main/dart/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 | 305 | 99.57
1189+
| 0055 |[Jump Game](src/main/dart/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) | 331 | 97.53
1190+
| 0041 |[First Missing Positive](src/main/dart/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) | 355 | 85.19
11731191

11741192
#### Udemy Two Pointers
11751193

11761194
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11771195
|-|-|-|-|-|-
1196+
| 0042 |[Trapping Rain Water](src/main/dart/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) | 328 | 80.77
11781197
| 0015 |[3Sum](src/main/dart/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) | 370 | 100.00
11791198

11801199
#### Udemy Famous Algorithm
11811200

11821201
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11831202
|-|-|-|-|-|-
1203+
| 0053 |[Maximum Subarray](src/main/dart/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) | 361 | 88.89
11841204

11851205
#### Udemy Sorting Algorithms
11861206

@@ -1191,6 +1211,7 @@
11911211

11921212
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11931213
|-|-|-|-|-|-
1214+
| 0048 |[Rotate Image](src/main/dart/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) | 311 | 90.24
11941215

11951216
#### Udemy Linked List
11961217

@@ -1226,7 +1247,9 @@
12261247
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12271248
|-|-|-|-|-|-
12281249
| 0022 |[Generate Parentheses](src/main/dart/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) | 312 | 79.37
1250+
| 0039 |[Combination Sum](src/main/dart/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 316 | 96.88
12291251
| 0017 |[Letter Combinations of a Phone Number](src/main/dart/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) | 293 | 92.50
1252+
| 0046 |[Permutations](src/main/dart/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 319 | 80.00
12301253

12311254
#### Udemy Bit Manipulation
12321255

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

12431266
| # | Title | Difficulty | Tag | Time, ms | Time, %
12441267
|------|----------------|-------------|-------------|----------|--------
1268+
| 0055 |[Jump Game](src/main/dart/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) | 331 | 97.53
1269+
| 0053 |[Maximum Subarray](src/main/dart/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) | 361 | 88.89
1270+
| 0051 |[N-Queens](src/main/dart/g0001_0100/s0051_n_queens)| Hard | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(N!)_Space_O(N) | 328 | 86.67
1271+
| 0049 |[Group Anagrams](src/main/dart/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) | 376 | 92.86
1272+
| 0048 |[Rotate Image](src/main/dart/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) | 311 | 90.24
1273+
| 0046 |[Permutations](src/main/dart/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!) | 319 | 80.00
1274+
| 0045 |[Jump Game II](src/main/dart/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) | 335 | 81.58
1275+
| 0042 |[Trapping Rain Water](src/main/dart/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) | 328 | 80.77
1276+
| 0041 |[First Missing Positive](src/main/dart/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) | 355 | 85.19
1277+
| 0039 |[Combination Sum](src/main/dart/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) | 316 | 96.88
12451278
| 0035 |[Search Insert Position](src/main/dart/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) | 325 | 80.73
12461279
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/dart/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) | 321 | 92.86
12471280
| 0033 |[Search in Rotated Sorted Array](src/main/dart/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) | 317 | 91.14

src/main/dart/g0001_0100/s0008_string_to_integer_atoi/readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ class Solution {
152152
// check owerflow
153153
if (sign == -1 && res < (MIN + digit) / 10) {
154154
return MIN;
155-
}
156-
else if (res > (MAX - digit) / 10) {
155+
} else if (res > (MAX - digit) / 10) {
157156
return MAX;
158157
}
159158
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/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+
```dart
49+
class Solution {
50+
List<List<int>> combinationSum(List<int> coins, int amount) {
51+
List<List<int>> ans = [];
52+
List<int> subList = [];
53+
combinationSumRec(coins.length, coins, amount, subList, ans);
54+
return ans;
55+
}
56+
57+
void combinationSumRec(int n, List<int> coins, int amount, List<int> subList, List<List<int>> ans) {
58+
if (amount == 0 || n == 0) {
59+
if (amount == 0) {
60+
// Create a new list from subList and add to ans
61+
ans.add(List.from(subList));
62+
}
63+
return;
64+
}
65+
66+
if (amount - coins[n - 1] >= 0) {
67+
subList.add(coins[n - 1]);
68+
combinationSumRec(n, coins, amount - coins[n - 1], subList, ans);
69+
// Remove the last element
70+
subList.removeLast();
71+
}
72+
73+
combinationSumRec(n - 1, coins, amount, subList, ans);
74+
}
75+
}
76+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/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+
```dart
44+
class Solution {
45+
int firstMissingPositive(List<int> nums) {
46+
int i = 0, n = nums.length;
47+
48+
while (i < n) {
49+
int j = nums[i] - 1;
50+
if (0 <= j && j < n && nums[i] != nums[j]) {
51+
// swap
52+
int tmp = nums[i];
53+
nums[i] = nums[j];
54+
nums[j] = tmp;
55+
} else {
56+
i++;
57+
}
58+
}
59+
60+
for (int i = 0; i < n; i++) {
61+
if (nums[i] != i + 1) {
62+
return i + 1;
63+
}
64+
}
65+
66+
return n + 1;
67+
}
68+
}
69+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/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+
```dart
35+
import 'dart:math';
36+
37+
class Solution {
38+
int trap(List<int> heigth) {
39+
int left = 0,
40+
right = heigth.length - 1,
41+
leftMax = 0,
42+
rightMax = 0,
43+
water = 0;
44+
45+
while (left < right) {
46+
leftMax = max(leftMax, heigth[left]);
47+
rightMax = max(rightMax, heigth[right]);
48+
water += leftMax < rightMax
49+
? leftMax - heigth[left++]
50+
: rightMax - heigth[right--];
51+
}
52+
53+
return water;
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)