Skip to content

Commit cf7b012

Browse files
authored
Added tasks 300-494
1 parent 39499ca commit cf7b012

File tree

21 files changed

+730
-0
lines changed

21 files changed

+730
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
4141
| 0003 |[Longest Substring Without Repeating Characters](src/main/rust/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
4242
| 0020 |[Valid Parentheses](src/main/rust/g0001_0100/s0020_valid_parentheses/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
4343
| 0005 |[Longest Palindromic Substring](src/main/rust/g0001_0100/s0005_longest_palindromic_substring/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 1 | 92.60
44+
| 0394 |[Decode String](src/main/rust/g0301_0400/s0394_decode_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
4445
| 0049 |[Group Anagrams](src/main/rust/g0001_0100/s0049_group_anagrams/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 0 | 100.00
4546

4647
#### Udemy Binary Search
@@ -110,6 +111,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
110111
|-|-|-|-|-|-
111112
| 0094 |[Binary Tree Inorder Traversal](src/main/rust/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
112113
| 0102 |[Binary Tree Level Order Traversal](src/main/rust/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 80.61
114+
| 0543 |[Diameter of Binary Tree](src/main/rust/g0501_0600/s0543_diameter_of_binary_tree/Solution.java)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 1 | 65.86
113115
| 0226 |[Invert Binary Tree](src/main/rust/g0201_0300/s0226_invert_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
114116
| 0104 |[Maximum Depth of Binary Tree](src/main/rust/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 1 | 80.46
115117
| 0124 |[Binary Tree Maximum Path Sum](src/main/rust/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.rs)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
@@ -137,6 +139,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
137139
| 0198 |[House Robber](src/main/rust/g0101_0200/s0198_house_robber/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
138140
| 0070 |[Climbing Stairs](src/main/rust/g0001_0100/s0070_climbing_stairs/Solution.rs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
139141
| 0064 |[Minimum Path Sum](src/main/rust/g0001_0100/s0064_minimum_path_sum/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
142+
| 0300 |[Longest Increasing Subsequence](src/main/rust/g0201_0300/s0300_longest_increasing_subsequence/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 0 | 100.00
140143
| 0072 |[Edit Distance](src/main/rust/g0001_0100/s0072_edit_distance/Solution.rs)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 0 | 100.00
141144
| 0010 |[Regular Expression Matching](src/main/rust/g0001_0100/s0010_regular_expression_matching/Solution.rs)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
142145

@@ -154,6 +157,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
154157

155158
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
156159
|-|-|-|-|-|-
160+
| 0338 |[Counting Bits](src/main/rust/g0301_0400/s0338_counting_bits/Solution.rs)| Easy | Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 0 | 100.00
157161

158162
#### Udemy Design
159163

@@ -365,6 +369,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
365369
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
366370
|-|-|-|-|-|-
367371
| 0215 |[Kth Largest Element in an Array](src/main/rust/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 10 | 90.24
372+
| 0347 |[Top K Frequent Elements](src/main/rust/g0301_0400/s0347_top_k_frequent_elements/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n\*log(n))_Space_O(k) | 0 | 100.00
368373

369374
#### Day 21 Heap Priority Queue
370375

@@ -486,6 +491,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
486491

487492
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
488493
|-|-|-|-|-|-
494+
| 0438 |[Find All Anagrams in a String](src/main/rust/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 2 | 89.29
489495

490496
#### Day 6 Breadth First Search Depth First Search
491497

@@ -552,6 +558,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
552558

553559
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
554560
|-|-|-|-|-|-
561+
| 0300 |[Longest Increasing Subsequence](src/main/rust/g0201_0300/s0300_longest_increasing_subsequence/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 0 | 100.00
555562

556563
#### Day 17 Dynamic Programming
557564

@@ -563,6 +570,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
563570
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
564571
|-|-|-|-|-|-
565572
| 0072 |[Edit Distance](src/main/rust/g0001_0100/s0072_edit_distance/Solution.rs)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 0 | 100.00
573+
| 0322 |[Coin Change](src/main/rust/g0301_0400/s0322_coin_change/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 3 | 100.00
566574

567575
#### Day 19 Bit Manipulation
568576

@@ -662,6 +670,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
662670

663671
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
664672
|-|-|-|-|-|-
673+
| 0300 |[Longest Increasing Subsequence](src/main/rust/g0201_0300/s0300_longest_increasing_subsequence/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 0 | 100.00
665674

666675
#### Day 4
667676

@@ -855,6 +864,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
855864

856865
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
857866
|-|-|-|-|-|-
867+
| 0300 |[Longest Increasing Subsequence](src/main/rust/g0201_0300/s0300_longest_increasing_subsequence/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 0 | 100.00
858868

859869
#### Day 19
860870

@@ -866,6 +876,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
866876

867877
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
868878
|-|-|-|-|-|-
879+
| 0322 |[Coin Change](src/main/rust/g0301_0400/s0322_coin_change/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 3 | 100.00
869880

870881
#### Day 21
871882

@@ -999,6 +1010,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
9991010

10001011
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10011012
|-|-|-|-|-|-
1013+
| 0438 |[Find All Anagrams in a String](src/main/rust/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 2 | 89.29
10021014

10031015
#### Day 13
10041016

@@ -1236,6 +1248,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12361248

12371249
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12381250
|-|-|-|-|-|-
1251+
| 0438 |[Find All Anagrams in a String](src/main/rust/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 2 | 89.29
12391252

12401253
#### Day 13 Hashmap
12411254

@@ -1247,6 +1260,7 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12471260

12481261
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12491262
|-|-|-|-|-|-
1263+
| 0394 |[Decode String](src/main/rust/g0301_0400/s0394_decode_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
12501264

12511265
#### Day 15 Heap
12521266

@@ -1293,6 +1307,8 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
12931307

12941308
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12951309
|-|-|-|-|-|-
1310+
| 0543 |[Diameter of Binary Tree](src/main/rust/g0501_0600/s0543_diameter_of_binary_tree/Solution.java)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 1 | 65.86
1311+
| 0437 |[Path Sum III](src/main/rust/g0401_0500/s0437_path_sum_iii/Solution.rs)| Medium | Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
12961312

12971313
#### Day 8 Binary Search
12981314

@@ -1322,11 +1338,13 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
13221338
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13231339
|-|-|-|-|-|-
13241340
| 0198 |[House Robber](src/main/rust/g0101_0200/s0198_house_robber/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1341+
| 0322 |[Coin Change](src/main/rust/g0301_0400/s0322_coin_change/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 3 | 100.00
13251342

13261343
#### Day 13 Dynamic Programming
13271344

13281345
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13291346
|-|-|-|-|-|-
1347+
| 0416 |[Partition Equal Subset Sum](src/main/rust/g0401_0500/s0416_partition_equal_subset_sum/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 20 | 74.07
13301348
| 0152 |[Maximum Product Subarray](src/main/rust/g0101_0200/s0152_maximum_product_subarray/Solution.rs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 71.23
13311349

13321350
#### Day 14 Sliding Window/Two Pointer
@@ -1376,6 +1394,16 @@ Rust-based LeetCode algorithm problem solutions, regularly updated.
13761394

13771395
| # | Title | Difficulty | Tag | Time, ms | Time, %
13781396
|------|----------------|-------------|-------------|----------|---------
1397+
| 0543 |[Diameter of Binary Tree](src/main/rust/g0501_0600/s0543_diameter_of_binary_tree/Solution.java)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 1 | 65.86
1398+
| 0494 |[Target Sum](src/main/rust/g0401_0500/s0494_target_sum/Solution.rs)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 3 | 83.33
1399+
| 0438 |[Find All Anagrams in a String](src/main/rust/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 2 | 89.29
1400+
| 0437 |[Path Sum III](src/main/rust/g0401_0500/s0437_path_sum_iii/Solution.rs)| Medium | Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1401+
| 0416 |[Partition Equal Subset Sum](src/main/rust/g0401_0500/s0416_partition_equal_subset_sum/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 20 | 74.07
1402+
| 0394 |[Decode String](src/main/rust/g0301_0400/s0394_decode_string/Solution.rs)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1403+
| 0347 |[Top K Frequent Elements](src/main/rust/g0301_0400/s0347_top_k_frequent_elements/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 0 | 100.00
1404+
| 0338 |[Counting Bits](src/main/rust/g0301_0400/s0338_counting_bits/Solution.rs)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 0 | 100.00
1405+
| 0322 |[Coin Change](src/main/rust/g0301_0400/s0322_coin_change/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(amount) | 3 | 100.00
1406+
| 0300 |[Longest Increasing Subsequence](src/main/rust/g0201_0300/s0300_longest_increasing_subsequence/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 0 | 100.00
13791407
| 0295 |[Find Median from Data Stream](src/main/rust/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.rs)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 99.02
13801408
| 0287 |[Find the Duplicate Number](src/main/rust/g0201_0300/s0287_find_the_duplicate_number/Solution.rs)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 4 | 98.95
13811409
| 0283 |[Move Zeroes](src/main/rust/g0201_0300/s0283_move_zeroes/Solution.rs)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Binary_Search
2+
// #Algorithm_II_Day_16_Dynamic_Programming #Binary_Search_II_Day_3 #Dynamic_Programming_I_Day_18
3+
// #Udemy_Dynamic_Programming #Big_O_Time_O(n*log_n)_Space_O(n)
4+
// #2024_09_12_Time_0_ms_(100.00%)_Space_2.1_MB_(100.00%)
5+
6+
impl Solution {
7+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
8+
if nums.is_empty() {
9+
return 0;
10+
}
11+
12+
let mut dp = vec![i32::MAX; nums.len() + 1];
13+
let (mut left, mut right) = (1, 1);
14+
15+
for &curr in nums.iter() {
16+
let (mut start, mut end) = (left, right);
17+
18+
// Binary search to find the position to update
19+
while start + 1 < end {
20+
let mid = start + (end - start) / 2;
21+
if dp[mid as usize] > curr {
22+
end = mid;
23+
} else {
24+
start = mid;
25+
}
26+
}
27+
28+
// Update the dp array
29+
if dp[start as usize] > curr {
30+
dp[start as usize] = curr;
31+
} else if curr > dp[start as usize] && curr < dp[end as usize] {
32+
dp[end as usize] = curr;
33+
} else if curr > dp[end as usize] {
34+
dp[end as usize + 1] = curr;
35+
right += 1;
36+
}
37+
}
38+
39+
right
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
300\. Longest Increasing Subsequence
2+
3+
Medium
4+
5+
Given an integer array `nums`, return the length of the longest strictly increasing subsequence.
6+
7+
A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, `[3,6,2,7]` is a subsequence of the array `[0,3,1,6,2,2,7]`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [10,9,2,5,3,7,101,18]
12+
13+
**Output:** 4
14+
15+
**Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [0,1,0,3,2,3]
20+
21+
**Output:** 4
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [7,7,7,7,7,7,7]
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `1 <= nums.length <= 2500`
32+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
33+
34+
**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search
2+
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
3+
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount)
4+
// #2024_09_12_Time_3_ms_(100.00%)_Space_2.2_MB_(69.95%)
5+
6+
impl Solution {
7+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
8+
let mut dp = vec![0; (amount + 1) as usize];
9+
dp[0] = 1; // Base case: one way to make 0 amount (using no coins)
10+
11+
for &coin in &coins {
12+
for i in coin..=amount {
13+
let prev = dp[(i - coin) as usize];
14+
if prev > 0 {
15+
if dp[i as usize] == 0 {
16+
dp[i as usize] = prev + 1;
17+
} else {
18+
dp[i as usize] = dp[i as usize].min(prev + 1);
19+
}
20+
}
21+
}
22+
}
23+
24+
// If the dp[amount] is still 0, it means no solution was found
25+
if dp[amount as usize] == 0 {
26+
-1
27+
} else {
28+
dp[amount as usize] - 1
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
322\. Coin Change
2+
3+
Medium
4+
5+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
6+
7+
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
**Example 1:**
12+
13+
**Input:** coins = [1,2,5], amount = 11
14+
15+
**Output:** 3
16+
17+
**Explanation:** 11 = 5 + 5 + 1
18+
19+
**Example 2:**
20+
21+
**Input:** coins = [2], amount = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** coins = [1], amount = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
* `1 <= coins.length <= 12`
34+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
35+
* <code>0 <= amount <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
2+
// #Big_O_Time_O(num)_Space_O(num) #2024_09_12_Time_0_ms_(100.00%)_Space_2.6_MB_(15.06%)
3+
4+
impl Solution {
5+
pub fn count_bits(num: i32) -> Vec<i32> {
6+
let mut result = vec![0; (num + 1) as usize];
7+
let mut border_pos = 1;
8+
let mut incr_pos = 1;
9+
10+
for i in 1..=num {
11+
// When we reach a power of 2, reset border_pos and incr_pos
12+
if incr_pos == border_pos {
13+
result[i as usize] = 1;
14+
incr_pos = 1;
15+
border_pos = i;
16+
} else {
17+
result[i as usize] = 1 + result[incr_pos as usize];
18+
incr_pos += 1;
19+
}
20+
}
21+
22+
result
23+
}
24+
}

0 commit comments

Comments
 (0)