Skip to content

Commit a378e5d

Browse files
committed
Add more solutions
1 parent 1bf1ee4 commit a378e5d

16 files changed

+754
-15
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
LeetCode is a website that has programming-related questions that are designed to be solved in a limited amount of time. This repository is a collection of some of my solutions written in [Rust](https://www.rust-lang.org/).
88

9-
## Solutions (49)
9+
## Solutions (62)
1010
| No. | Title | Solution | Problem | Difficulty |
1111
|:---:|:------|:--------:|:-------:|:----------:|
1212
| 1 | Two Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/two_sum.rs) | [Leetcode](https://leetcode.com/problems/two-sum/) | Easy |
@@ -16,38 +16,51 @@ LeetCode is a website that has programming-related questions that are designed t
1616
| 49 | Group Anagrams | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/group_anagrams.rs) | [Leetcode](https://leetcode.com/problems/group-anagrams/) | Medium |
1717
| 54 | Spiral Matrix | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/spiral_order.rs) | [Leetcode](https://leetcode.com/problems/spiral-matrix/) | Medium |
1818
| 55 | Jump Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/dynamic_programming/can_jump.rs) | [Leetcode](https://leetcode.com/problems/jump-game/) | Medium |
19+
| 62 | Unique Paths | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/unique_paths.rs) | [Leetcode](https://leetcode.com/problems/unique-paths/) | Medium |
1920
| 72 | Edit Distance | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_distance.rs) | [Leetcode](https://leetcode.com/problems/edit-distance/) | Hard |
2021
| 73 | Set Matrix Zeroes | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/set_zeroes.rs) | [Leetcode](https://leetcode.com/problems/set-matrix-zeroes/submissions/) | Medium |
2122
| 75 | Sort Colors | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/sorting/sort_colors.rs) | [Leetcode](https://leetcode.com/problems/sort-colors/) | Medium |
23+
| 94 | Binary Tree Inorder Traversal | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/inorder_traversal.rs) | [Leetcode](https://leetcode.com/problems/binary-tree-inorder-traversal/) | Easy |
24+
| 100 | Same Tree | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_same_tree.rs) | [Leetcode](https://leetcode.com/problems/same-tree/) | easy |
25+
| 103 | Binary Tree Zigzag Level Order Traversal | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/zigzag_level_order.rs) | [Leetcode](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | Medium |
2226
| 115 | Distinct Subsequences | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/num_distinct.rs) | [Leetcode](https://leetcode.com/problems/distinct-subsequences/) | Hard |
27+
| 134 | Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/can_complete_circuit.rs) | [Leetcode](https://leetcode.com/problems/gas-station/) | Medium |
2328
| 169 | Majority Element | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/others/majority_element.rs) | [Leetcode](https://leetcode.com/problems/majority-element/solution/) | Easy |
2429
| 174 | Dungeon Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/calculate_minimum_hp.rs) | [Leetcode](https://leetcode.com/problems/dungeon-game/) | Hard |
30+
| 200 | Number of Islands | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_islands.rs) | [Leetcode](https://leetcode.com/problems/number-of-islands/) | Medium |
31+
| 230 | Kth Smallest Element in a BST | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/kth_smallest.rs) | [Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | Medium |
2532
| 242 | Valid Anagram | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_anagram.rs) | [Leetcode](https://leetcode.com/problems/valid-anagram/) | Easy |
2633
| 282 | Expression Add Operators | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/add_operators.rs) | [Leetcode](https://leetcode.com/problems/expression-add-operators/) | Hard |
2734
| 317 | Shortest Distance from All Buildings | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_distance.rs) | [Leetcode](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | Hard |
35+
| 322 | Coin Change | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/coin_change.rs) | [Leetcode](https://leetcode.com/problems/coin-change/) | Medium |
2836
| 325 | Maximum Size Subarray Sum Equals k | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/max_sub_array_len.rs) | [Leetcode](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/) | Medium |
2937
| 334 | Increasing Triplet Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/increasing_triplet.rs) | [Leetcode](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium |
3038
| 350 | Intersection of Two Arrays II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/intersect.rs) | [Leetcode](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | Easy |
3139
| 368 | Largest Divisible Subset | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/largest_divisible_subset.rs) | [Leetcode](https://leetcode.com/problems/largest-divisible-subset/) | Medium |
3240
| 435 | Non-overlapping Intervals | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/erase_overlap_intervals.rs) | [Leetcode](https://leetcode.com/problems/non-overlapping-intervals/) | Medium |
3341
| 452 | Minimum Number of Arrows to Burst Balloons | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_min_arrow_shots.rs) | [Leetcode](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | Medium |
42+
| 463 | Island Perimeter | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/island_perimeter.rs) | [Leetcode](https://leetcode.com/problems/island-perimeter/) | Easy |
3443
| 485 | Max Consecutive Ones | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/find_max_consecutive_ones.rs) | [Leetcode](https://leetcode.com/problems/max-consecutive-ones/) | Easy |
3544
| 495 | Teemo Attacking | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_poisoned_duration.rs) | [Leetcode](https://leetcode.com/problems/teemo-attacking/) | Easy |
3645
| 698 | Partition to K Equal Sum Subsets | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/can_partition_k_subsets.rs) | [Leetcode](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) | Medium |
3746
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
3847
| 725 | Split Linked List in Parts | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/split_list_to_parts.rs) | [Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/) | Medium |
3948
| 741 | Cherry Pickup | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/cherry_pickup.rs) | [Leetcode](https://leetcode.com/problems/cherry-pickup/) | Hard |
49+
| 749 | Contain Virus | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/contain_virus.rs) | [Leetcode](https://leetcode.com/problems/contain-virus/) | Hard |
4050
| 774 | Minimize Max Distance to Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/minmax_gas_dist.rs) | [Leetcode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | Medium |
4151
| 875 | Koko Eating Bananas | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_eating_speed.rs) | [Leetcode](https://leetcode.com/problems/koko-eating-bananas/) | Medium |
4252
| 922 | Sort Array By Parity II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/sort_array_by_parity_ii.rs) | [Leetcode](https://leetcode.com/problems/sort-array-by-parity-ii/) | Easy |
4353
| 929 | Unique Email Addresses | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/num_unique_emails.rs) | [Leetcode](https://leetcode.com/problems/unique-email-addresses/) | Easy |
54+
| 991 | Broken Calculator | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/broken_calc.rs) | [Leetcode](https://leetcode.com/problems/broken-calculator/) | Medium |
4455
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
4556
| 1134 | Armstrong Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_good_array.rs) | [Leetcode](https://leetcode.com/problems/armstrong-number/) | Easy |
4657
| 1137 | N-th Tribonacci Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tribonacci.rs) | [Leetcode](https://leetcode.com/problems/n-th-tribonacci-number/) | Easy |
4758
| 1143 | Longest Common Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/longest_common_subsequence.rs) | [Leetcode](https://leetcode.com/problems/longest-common-subsequence/) | Medium |
59+
| 1161 | Maximum Level Sum of a Binary Tree | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_level_sum.rs) | [Leetcode](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | Medium |
4860
| 1239 | Maximum Length of a Concatenated String with Unique Characters | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/max_length.rs) | [Leetcode](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | Medium |
4961
| 1275 | Find Winner on a Tic Tac Toe Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tictactoe.rs) | [Leetcode](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | Easy |
5062
| 1293 | Shortest Path in a Grid with Obstacles Elimination | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_path.rs) | [Leetcode](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) | Hard |
63+
| 1315 | Sum of Nodes with Even-Valued Grandparent | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/sum_even_grandparent.rs) | [Leetcode](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/) | Medium |
5164
| 1328 | Break a Palindrome | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/break_palindrome.rs) | [Leetcode](https://leetcode.com/problems/break-a-palindrome/) | Medium |
5265
| 1457 | Pseudo-Palindromic Paths in a Binary Tree | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/pseudo_palindromic_paths.rs) | [Leetcode](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | Easy |
5366
| 1801 | Number of Orders in the Backlog | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/get_number_of_backlog_orders.rs) | [Leetcode](https://leetcode.com/problems/number-of-orders-in-the-backlog/) | Medium |

src/leetcode/problem/broken_calc.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// 991. Broken Calculator, Medium
2+
// https://leetcode.com/problems/broken-calculator/
3+
impl Solution {
4+
pub fn broken_calc(start_value: i32, target: i32) -> i32 {
5+
let mut ops = 0;
6+
let mut value = target;
7+
8+
while value > start_value {
9+
if value % 2 == 1 {
10+
value += 1;
11+
ops += 1;
12+
}
13+
14+
value /= 2;
15+
ops += 1;
16+
}
17+
18+
ops += start_value - value;
19+
ops
20+
}
21+
}
22+
23+
struct Solution {}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
use crate::vec_vec_i32;
29+
30+
#[test]
31+
fn test_broken_calc() {
32+
assert_eq!(Solution::broken_calc(2, 3), 2);
33+
}
34+
35+
#[test]
36+
fn test_broken_calc2() {
37+
assert_eq!(Solution::broken_calc(5, 8), 2);
38+
}
39+
40+
#[test]
41+
fn test_broken_calc3() {
42+
assert_eq!(Solution::broken_calc(3, 10), 3);
43+
}
44+
45+
#[test]
46+
fn test_broken_calc4() {
47+
assert_eq!(Solution::broken_calc(1024, 1), 1023);
48+
}
49+
50+
#[test]
51+
fn test_broken_calc5() {
52+
assert_eq!(Solution::broken_calc(1, 1000000000), 39);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 134. Gas Station, Medium
2+
// https://leetcode.com/problems/gas-station/
3+
impl Solution {
4+
pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
5+
let n = gas.len();
6+
7+
for i in 0..n {
8+
let mut pos = i;
9+
let mut dist = 0;
10+
let mut tank = 0;
11+
12+
while dist <= n {
13+
tank += gas[pos] - cost[pos];
14+
if tank < 0 {
15+
break;
16+
} else {
17+
pos += 1;
18+
pos %= n;
19+
dist += 1;
20+
}
21+
}
22+
23+
if dist >= n {
24+
return i as i32;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
}
31+
32+
struct Solution {}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
use crate::vec_vec_i32;
38+
39+
#[test]
40+
fn test_can_complete_circuit() {
41+
assert_eq!(Solution::can_complete_circuit(vec![1, 2, 3, 4, 5], vec![3, 4, 5, 1, 2]), 3);
42+
}
43+
44+
#[test]
45+
fn test_can_complete_circuit2() {
46+
assert_eq!(Solution::can_complete_circuit(vec![2, 3, 4], vec![3, 4, 3]), -1);
47+
}
48+
}

src/leetcode/problem/coin_change.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// 322. Coin Change, Medium
2+
// https://leetcode.com/problems/coin-change/
3+
impl Solution {
4+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
5+
let n = coins.len();
6+
let mut dp = vec![amount + 1; amount as usize + 1];
7+
8+
dp[0] = 0;
9+
10+
for i in 1..=amount {
11+
for coin in coins.iter() {
12+
if i >= *coin {
13+
dp[i as usize] = dp[i as usize].min(dp[(i - *coin) as usize] + 1);
14+
}
15+
}
16+
}
17+
18+
if dp.last().unwrap() > &amount {
19+
-1
20+
} else {
21+
*dp.last().unwrap() as i32
22+
}
23+
}
24+
}
25+
26+
struct Solution {}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
use crate::vec_vec_i32;
32+
33+
#[test]
34+
fn test_coin_change() {
35+
assert_eq!(Solution::coin_change(vec![1, 2, 5], 11), 3);
36+
}
37+
38+
#[test]
39+
fn test_coin_change2() {
40+
assert_eq!(Solution::coin_change(vec![2], 3), -1);
41+
}
42+
43+
#[test]
44+
fn test_coin_change3() {
45+
assert_eq!(Solution::coin_change(vec![1], 0), 0);
46+
}
47+
48+
#[test]
49+
fn test_coin_change4() {
50+
assert_eq!(Solution::coin_change(vec![1], 1), 1);
51+
}
52+
53+
#[test]
54+
fn test_coin_change5() {
55+
assert_eq!(Solution::coin_change(vec![1], 2), 2);
56+
}
57+
}

src/leetcode/problem/contain_virus.rs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use std::collections::VecDeque;
2+
3+
// 749. Contain Virus, Hard
4+
// https://leetcode.com/problems/contain-virus/
5+
impl Solution {
6+
pub fn contain_virus(mut is_infected: Vec<Vec<i32>>) -> i32 {
7+
fn find_regions(grid: &mut Vec<Vec<i32>>, start: (i32, i32)) -> (Vec<(i32, i32)>, Vec<(i32, i32)>) {
8+
let mut regions = Vec::<(i32, i32)>::new();
9+
let mut neighbors = Vec::<(i32, i32)>::new();
10+
let directions = vec![(0, 1), (0, -1), (1, 0), (-1, 0)];
11+
12+
let mut q = VecDeque::<(i32, i32)>::new();
13+
q.push_back(start);
14+
15+
while !q.is_empty() {
16+
let pos = q.pop_back().unwrap();
17+
18+
if grid[pos.0 as usize][pos.1 as usize] == 1 {
19+
grid[pos.0 as usize][pos.1 as usize] = 2;
20+
regions.push(pos);
21+
for direction in directions.iter() {
22+
let next = (pos.0 + direction.0, pos.1 + direction.1);
23+
if next.0 < 0 || next.1 < 0 || next.0 >= grid.len() as i32 || next.1 >= grid[0].len() as i32 {
24+
continue;
25+
}
26+
q.push_back(next);
27+
}
28+
} else if grid[pos.0 as usize][pos.1 as usize] == 0 {
29+
neighbors.push(pos);
30+
}
31+
}
32+
33+
(regions, neighbors)
34+
}
35+
36+
let mut walls = 0;
37+
let [n, m] = [is_infected.len(), is_infected[0].len()];
38+
let mut sim = true;
39+
40+
while sim {
41+
sim = false;
42+
43+
let mut state = is_infected.clone();
44+
let mut state_regions = Vec::<(Vec<(i32, i32)>, Vec<(i32, i32)>)>::new();
45+
for i in 0..n {
46+
for j in 0..m {
47+
if state[i][j] == 1 {
48+
let (regions, neighbors) = find_regions(&mut state, (i as i32, j as i32));
49+
state_regions.push((regions, neighbors));
50+
}
51+
}
52+
}
53+
54+
// find biggest threat
55+
state_regions.sort_unstable_by(|a, b| a.1.len().cmp(&b.1.len()));
56+
57+
// build walls
58+
if !state_regions.is_empty() {
59+
sim = true;
60+
let biggest_threat = state_regions.pop().unwrap();
61+
walls += biggest_threat.1.len() as i32;
62+
for pos in biggest_threat.0 {
63+
is_infected[pos.0 as usize][pos.1 as usize] = 2;
64+
}
65+
}
66+
67+
// spread the virus
68+
for threat in state_regions {
69+
for pos in threat.1 {
70+
is_infected[pos.0 as usize][pos.1 as usize] = 1;
71+
}
72+
}
73+
}
74+
75+
walls
76+
}
77+
}
78+
79+
struct Solution {}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
use crate::vec_vec_i32;
85+
86+
#[test]
87+
fn test_is_infected() {
88+
assert_eq!(
89+
Solution::contain_virus(vec_vec_i32![
90+
[0, 1, 0, 0, 0, 0, 0, 1],
91+
[0, 1, 0, 0, 0, 0, 0, 1],
92+
[0, 0, 0, 0, 0, 0, 0, 1],
93+
[0, 0, 0, 0, 0, 0, 0, 0]
94+
]),
95+
10
96+
);
97+
}
98+
99+
#[test]
100+
fn test_is_infected2() {
101+
assert_eq!(Solution::contain_virus(vec_vec_i32![[1, 1, 1], [1, 0, 1], [1, 1, 1]]), 4);
102+
}
103+
104+
#[test]
105+
fn test_is_infected3() {
106+
assert_eq!(
107+
Solution::contain_virus(vec_vec_i32![[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0]]),
108+
13
109+
);
110+
}
111+
112+
#[test]
113+
fn test_is_infected4() {
114+
assert_eq!(
115+
Solution::contain_virus(vec_vec_i32![[0, 1, 0, 0, 0, 0, 0, 1], [0, 1, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1]]),
116+
16
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)