Skip to content

Commit a3b736a

Browse files
committed
Added tasks 300-1143
1 parent 9c6e6ef commit a3b736a

File tree

16 files changed

+1102
-0
lines changed

16 files changed

+1102
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 300\. Longest Increasing Subsequence
5+
6+
Medium
7+
8+
Given an integer array `nums`, return the length of the longest strictly increasing subsequence.
9+
10+
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]`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [10,9,2,5,3,7,101,18]
15+
16+
**Output:** 4
17+
18+
**Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [0,1,0,3,2,3]
23+
24+
**Output:** 4
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [7,7,7,7,7,7,7]
29+
30+
**Output:** 1
31+
32+
**Constraints:**
33+
34+
* `1 <= nums.length <= 2500`
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
37+
**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity?
38+
39+
## Solution
40+
41+
```rust
42+
impl Solution {
43+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
44+
if nums.is_empty() {
45+
return 0;
46+
}
47+
48+
let mut dp = vec![i32::MAX; nums.len() + 1];
49+
let (mut left, mut right) = (1, 1);
50+
51+
for &curr in nums.iter() {
52+
let (mut start, mut end) = (left, right);
53+
54+
// Binary search to find the position to update
55+
while start + 1 < end {
56+
let mid = start + (end - start) / 2;
57+
if dp[mid as usize] > curr {
58+
end = mid;
59+
} else {
60+
start = mid;
61+
}
62+
}
63+
64+
// Update the dp array
65+
if dp[start as usize] > curr {
66+
dp[start as usize] = curr;
67+
} else if curr > dp[start as usize] && curr < dp[end as usize] {
68+
dp[end as usize] = curr;
69+
} else if curr > dp[end as usize] {
70+
dp[end as usize + 1] = curr;
71+
right += 1;
72+
}
73+
}
74+
75+
right
76+
}
77+
}
78+
```
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-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 322\. Coin Change
5+
6+
Medium
7+
8+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
9+
10+
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`.
11+
12+
You may assume that you have an infinite number of each kind of coin.
13+
14+
**Example 1:**
15+
16+
**Input:** coins = [1,2,5], amount = 11
17+
18+
**Output:** 3
19+
20+
**Explanation:** 11 = 5 + 5 + 1
21+
22+
**Example 2:**
23+
24+
**Input:** coins = [2], amount = 3
25+
26+
**Output:** -1
27+
28+
**Example 3:**
29+
30+
**Input:** coins = [1], amount = 0
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* `1 <= coins.length <= 12`
37+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
38+
* <code>0 <= amount <= 10<sup>4</sup></code>
39+
40+
## Solution
41+
42+
```rust
43+
impl Solution {
44+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
45+
let mut dp = vec![0; (amount + 1) as usize];
46+
dp[0] = 1; // Base case: one way to make 0 amount (using no coins)
47+
48+
for &coin in &coins {
49+
for i in coin..=amount {
50+
let prev = dp[(i - coin) as usize];
51+
if prev > 0 {
52+
if dp[i as usize] == 0 {
53+
dp[i as usize] = prev + 1;
54+
} else {
55+
dp[i as usize] = dp[i as usize].min(prev + 1);
56+
}
57+
}
58+
}
59+
}
60+
61+
// If the dp[amount] is still 0, it means no solution was found
62+
if dp[amount as usize] == 0 {
63+
-1
64+
} else {
65+
dp[amount as usize] - 1
66+
}
67+
}
68+
}
69+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 338\. Counting Bits
5+
6+
Easy
7+
8+
Given an integer `n`, return _an array_ `ans` _of length_ `n + 1` _such that for each_ `i` (`0 <= i <= n`)_,_ `ans[i]` _is the **number of**_ `1`_**'s** in the binary representation of_ `i`.
9+
10+
**Example 1:**
11+
12+
**Input:** n = 2
13+
14+
**Output:** [0,1,1]
15+
16+
**Explanation:**
17+
18+
0 --> 0
19+
1 --> 1
20+
2 --> 10
21+
22+
**Example 2:**
23+
24+
**Input:** n = 5
25+
26+
**Output:** [0,1,1,2,1,2]
27+
28+
**Explanation:**
29+
30+
0 --> 0
31+
1 --> 1
32+
2 --> 10
33+
3 --> 11
34+
4 --> 100
35+
5 --> 101
36+
37+
**Constraints:**
38+
39+
* <code>0 <= n <= 10<sup>5</sup></code>
40+
41+
**Follow up:**
42+
43+
* It is very easy to come up with a solution with a runtime of `O(n log n)`. Can you do it in linear time `O(n)` and possibly in a single pass?
44+
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)?
45+
46+
## Solution
47+
48+
```rust
49+
impl Solution {
50+
pub fn count_bits(num: i32) -> Vec<i32> {
51+
let mut result = vec![0; (num + 1) as usize];
52+
let mut border_pos = 1;
53+
let mut incr_pos = 1;
54+
55+
for i in 1..=num {
56+
// When we reach a power of 2, reset border_pos and incr_pos
57+
if incr_pos == border_pos {
58+
result[i as usize] = 1;
59+
incr_pos = 1;
60+
border_pos = i;
61+
} else {
62+
result[i as usize] = 1 + result[incr_pos as usize];
63+
incr_pos += 1;
64+
}
65+
}
66+
67+
result
68+
}
69+
}
70+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 347\. Top K Frequent Elements
5+
6+
Medium
7+
8+
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,1,1,2,2,3], k = 2
13+
14+
**Output:** [1,2]
15+
16+
**Example 2:**
17+
18+
**Input:** nums = [1], k = 1
19+
20+
**Output:** [1]
21+
22+
**Constraints:**
23+
24+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
25+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
26+
* `k` is in the range `[1, the number of unique elements in the array]`.
27+
* It is **guaranteed** that the answer is **unique**.
28+
29+
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size.
30+
31+
## Solution
32+
33+
```rust
34+
use std::collections::HashMap;
35+
36+
impl Solution {
37+
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
38+
nums.iter().fold(HashMap::new(), |mut map, n| {
39+
let mut counter = map.entry(n).or_insert(0);
40+
*counter += 1;
41+
map
42+
})
43+
.drain()
44+
.fold(vec![(0, 0); k as usize], |mut top_k, (&num, count)| {
45+
if count > top_k[0].1 {
46+
top_k[0] = (num, count);
47+
48+
let mut next_index = 1;
49+
while next_index < k as usize {
50+
if count > top_k[next_index].1 {
51+
let temp = top_k[next_index];
52+
top_k[next_index] = (num, count);
53+
top_k[next_index - 1] = temp;
54+
}
55+
next_index += 1;
56+
}
57+
}
58+
59+
top_k
60+
})
61+
.into_iter()
62+
.map(|(num, count)| num)
63+
.collect()
64+
}
65+
}
66+
```
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-Rust/LeetCode-in-Rust?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Rust/LeetCode-in-Rust?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork)
3+
4+
## 394\. Decode String
5+
6+
Medium
7+
8+
Given an encoded string, return its decoded string.
9+
10+
The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer.
11+
12+
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, `k`. For example, there will not be input like `3a` or `2[4]`.
13+
14+
The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "3[a]2[bc]"
19+
20+
**Output:** "aaabcbc"
21+
22+
**Example 2:**
23+
24+
**Input:** s = "3[a2[c]]"
25+
26+
**Output:** "accaccacc"
27+
28+
**Example 3:**
29+
30+
**Input:** s = "2[abc]3[cd]ef"
31+
32+
**Output:** "abcabccdcdcdef"
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 30`
37+
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
38+
* `s` is guaranteed to be **a valid** input.
39+
* All the integers in `s` are in the range `[1, 300]`.
40+
41+
## Solution
42+
43+
```rust
44+
impl Solution {
45+
pub fn decode_string(s: String) -> String {
46+
s
47+
.chars()
48+
.fold((String::new(), String::new(), Vec::new()), |(mut next_string, mut next_integer, mut stack): (String, String, Vec<(usize, String)>), c| {
49+
match c {
50+
'0'..='9' => {
51+
next_integer.push(c);
52+
(next_string, next_integer, stack)
53+
},
54+
'[' => {
55+
stack.push((next_integer.parse::<usize>().unwrap(), next_string));
56+
next_integer.clear();
57+
(String::new(), next_integer, stack)
58+
},
59+
']' => {
60+
let (last_integer, left_string) = stack.pop().unwrap();
61+
let next_string = left_string + &next_string.repeat(last_integer);
62+
(next_string, next_integer, stack)
63+
},
64+
c => {
65+
next_string.push(c);
66+
(next_string, next_integer, stack)
67+
}
68+
}
69+
}).0
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)