Skip to content

Commit 2076e43

Browse files
committed
Added tasks 56-78
1 parent 69af8b5 commit 2076e43

File tree

11 files changed

+751
-0
lines changed

11 files changed

+751
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
4747
|-|-|-|-|-|-
48+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
4849

4950
#### Day 6 String
5051

@@ -105,6 +106,8 @@
105106

106107
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
107108
|-|-|-|-|-|-
109+
| 0075 |[Sort Colors](src/main/dart/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 314 | 79.31
110+
| 0056 |[Merge Intervals](src/main/dart/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 376 | 100.00
108111

109112
#### Day 3 Array
110113

@@ -273,6 +276,7 @@
273276

274277
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
275278
|-|-|-|-|-|-
279+
| 0070 |[Climbing Stairs](src/main/dart/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 301 | 81.01
276280

277281
#### Day 13 Bit Manipulation
278282

@@ -292,6 +296,7 @@
292296
|-|-|-|-|-|-
293297
| 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, Big_O_Time_O(log_n)_Space_O(1) | 321 | 92.86
294298
| 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, Big_O_Time_O(log_n)_Space_O(1) | 317 | 91.14
299+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
295300

296301
#### Day 2 Binary Search
297302

@@ -334,6 +339,7 @@
334339

335340
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
336341
|-|-|-|-|-|-
342+
| 0078 |[Subsets](src/main/dart/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 296 | 96.88
337343

338344
#### Day 10 Recursion Backtracking
339345

@@ -359,6 +365,7 @@
359365
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
360366
|-|-|-|-|-|-
361367
| 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
368+
| 0062 |[Unique Paths](src/main/dart/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 287 | 91.67
362369

363370
#### Day 14 Dynamic Programming
364371

@@ -385,6 +392,7 @@
385392

386393
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
387394
|-|-|-|-|-|-
395+
| 0072 |[Edit Distance](src/main/dart/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 333 | 100.00
388396

389397
#### Day 19 Bit Manipulation
390398

@@ -444,6 +452,7 @@
444452

445453
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
446454
|-|-|-|-|-|-
455+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
447456

448457
#### Day 9
449458

@@ -579,6 +588,7 @@
579588

580589
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
581590
|-|-|-|-|-|-
591+
| 0070 |[Climbing Stairs](src/main/dart/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 301 | 81.01
582592

583593
#### Day 3
584594

@@ -648,11 +658,13 @@
648658

649659
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
650660
|-|-|-|-|-|-
661+
| 0062 |[Unique Paths](src/main/dart/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 287 | 91.67
651662

652663
#### Day 16
653664

654665
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
655666
|-|-|-|-|-|-
667+
| 0064 |[Minimum Path Sum](src/main/dart/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 328 | 88.89
656668

657669
#### Day 17
658670

@@ -669,6 +681,7 @@
669681

670682
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
671683
|-|-|-|-|-|-
684+
| 0072 |[Edit Distance](src/main/dart/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 333 | 100.00
672685

673686
#### Day 20
674687

@@ -1023,11 +1036,13 @@
10231036

10241037
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10251038
|-|-|-|-|-|-
1039+
| 0070 |[Climbing Stairs](src/main/dart/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 301 | 81.01
10261040

10271041
#### Day 11 Dynamic Programming
10281042

10291043
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10301044
|-|-|-|-|-|-
1045+
| 0062 |[Unique Paths](src/main/dart/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 287 | 91.67
10311046

10321047
#### Day 12 Sliding Window/Two Pointer
10331048

@@ -1092,6 +1107,7 @@
10921107

10931108
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10941109
|-|-|-|-|-|-
1110+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
10951111
| 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, Big_O_Time_O(log_n)_Space_O(1) | 317 | 91.14
10961112

10971113
#### Day 9 Binary Search Tree
@@ -1124,6 +1140,7 @@
11241140
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11251141
|-|-|-|-|-|-
11261142
| 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
1143+
| 0076 |[Minimum Window Substring](src/main/dart/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 337 | 92.86
11271144

11281145
#### Day 15 Tree
11291146

@@ -1139,6 +1156,7 @@
11391156

11401157
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11411158
|-|-|-|-|-|-
1159+
| 0056 |[Merge Intervals](src/main/dart/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 376 | 100.00
11421160

11431161
#### Day 18 Stack
11441162

@@ -1187,6 +1205,7 @@
11871205
|-|-|-|-|-|-
11881206
| 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
11891207
| 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
1208+
| 0075 |[Sort Colors](src/main/dart/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 314 | 79.31
11901209
| 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
11911210

11921211
#### Udemy Two Pointers
@@ -1211,7 +1230,10 @@
12111230

12121231
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12131232
|-|-|-|-|-|-
1233+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
12141234
| 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
1235+
| 0073 |[Set Matrix Zeroes](src/main/dart/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 350 | 91.30
1236+
| 0056 |[Merge Intervals](src/main/dart/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 376 | 100.00
12151237

12161238
#### Udemy Linked List
12171239

@@ -1240,6 +1262,9 @@
12401262

12411263
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12421264
|-|-|-|-|-|-
1265+
| 0070 |[Climbing Stairs](src/main/dart/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 301 | 81.01
1266+
| 0064 |[Minimum Path Sum](src/main/dart/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 328 | 88.89
1267+
| 0072 |[Edit Distance](src/main/dart/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 333 | 100.00
12431268
| 0010 |[Regular Expression Matching](src/main/dart/g0001_0100/s0010_regular_expression_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 341 | 81.82
12441269

12451270
#### Udemy Backtracking/Recursion
@@ -1248,6 +1273,7 @@
12481273
|-|-|-|-|-|-
12491274
| 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
12501275
| 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
1276+
| 0078 |[Subsets](src/main/dart/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 296 | 96.88
12511277
| 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
12521278
| 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
12531279

@@ -1265,6 +1291,16 @@
12651291

12661292
| # | Title | Difficulty | Tag | Time, ms | Time, %
12671293
|------|----------------|-------------|-------------|----------|--------
1294+
| 0078 |[Subsets](src/main/dart/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 296 | 96.88
1295+
| 0076 |[Minimum Window Substring](src/main/dart/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Big_O_Time_O(s.length())_Space_O(1) | 337 | 92.86
1296+
| 0075 |[Sort Colors](src/main/dart/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 314 | 79.31
1297+
| 0074 |[Search a 2D Matrix](src/main/dart/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 321 | 90.28
1298+
| 0073 |[Set Matrix Zeroes](src/main/dart/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 350 | 91.30
1299+
| 0072 |[Edit Distance](src/main/dart/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 333 | 100.00
1300+
| 0070 |[Climbing Stairs](src/main/dart/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 301 | 81.01
1301+
| 0064 |[Minimum Path Sum](src/main/dart/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 328 | 88.89
1302+
| 0062 |[Unique Paths](src/main/dart/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 287 | 91.67
1303+
| 0056 |[Merge Intervals](src/main/dart/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix, Big_O_Time_O(n_log_n)_Space_O(n) | 376 | 100.00
12681304
| 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
12691305
| 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
12701306
| 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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
## 56\. Merge Intervals
5+
6+
Medium
7+
8+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
9+
10+
**Example 1:**
11+
12+
**Input:** intervals = \[\[1,3],[2,6],[8,10],[15,18]]
13+
14+
**Output:** [[1,6],[8,10],[15,18]]
15+
16+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
17+
18+
**Example 2:**
19+
20+
**Input:** intervals = \[\[1,4],[4,5]]
21+
22+
**Output:** [[1,5]]
23+
24+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
25+
26+
**Constraints:**
27+
28+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
29+
* `intervals[i].length == 2`
30+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
31+
32+
## Solution
33+
34+
```dart
35+
class Solution {
36+
List<List<int>> merge(List<List<int>> intervals) {
37+
// Sort the intervals based on the start time
38+
intervals.sort((a, b) =>a[0].compareTo(b[0]));
39+
40+
List<List<int>> merged = [];
41+
List<int> current = intervals[0];
42+
merged.add(current);
43+
44+
for (List<int> next in intervals){
45+
if (current[1] >= next[0]) {
46+
// If the intervals overlap, merge them
47+
current[1] = current[1] > next[1] ? current[1] : next[1];
48+
} else {
49+
// Otherwise, move to the next interval
50+
current = next;
51+
merged.add(current);
52+
}
53+
}
54+
55+
return merged;
56+
}
57+
}
58+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
## 62\. Unique Paths
5+
6+
Medium
7+
8+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
9+
10+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
11+
12+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
17+
18+
**Input:** m = 3, n = 7
19+
20+
**Output:** 28
21+
22+
**Example 2:**
23+
24+
**Input:** m = 3, n = 2
25+
26+
**Output:** 3
27+
28+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
29+
30+
1. Right -> Down -> Down
31+
32+
2. Down -> Down -> Right
33+
34+
3. Down -> Right -> Down
35+
36+
**Constraints:**
37+
38+
* `1 <= m, n <= 100`
39+
40+
## Solution
41+
42+
```dart
43+
class Solution {
44+
int uniquePaths(int m, int n) {
45+
List<List<int>> dp = List.generate(m, (_) => List.filled(n, 0));
46+
47+
// Initialize the first column to 1
48+
for (int i = 0; i < m; i++) {
49+
dp[i][0] = 1;
50+
}
51+
52+
// Initialize the first row to 1
53+
for (int j = 0; j < n; j++) {
54+
dp[0][j] = 1;
55+
}
56+
57+
// Fill the dp table
58+
for (int i = 1; i < m; i++) {
59+
for (int j = 1; j < n; j++) {
60+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
61+
}
62+
}
63+
64+
return dp[m - 1][n - 1];
65+
}
66+
}
67+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
## Solution
36+
37+
```dart
38+
class Solution {
39+
int minPathSum(List<List<int>> grid) {
40+
int m = grid.length;
41+
int n = grid[0].length;
42+
43+
List<int> pathSum = List.filled(n, 100000);
44+
pathSum[0] = 0;
45+
46+
for (int y = 0; y < m; y++) {
47+
for (int x = 0; x < n; x++) {
48+
pathSum[x] =
49+
(x > 0 ? min(pathSum[x], pathSum[x - 1]) : pathSum[x]) + grid[y][x];
50+
}
51+
}
52+
return pathSum[n - 1];
53+
}
54+
}
55+
56+
int min(int a, int b) {
57+
if (a < b) {
58+
return a;
59+
}
60+
return b;
61+
}
62+
```

0 commit comments

Comments
 (0)