Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31e83c6

Browse files
committed
Add more solutions
1 parent 8dde600 commit 31e83c6

File tree

5 files changed

+194
-2
lines changed

5 files changed

+194
-2
lines changed

β€ŽREADME.md

Lines changed: 6 additions & 2 deletions
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 (76)
9+
## Solutions (80)
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 |
@@ -49,6 +49,7 @@ LeetCode is a website that has programming-related questions that are designed t
4949
| 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 |
5050
| 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 |
5151
| 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 |
52+
| 547 | Number of Provinces | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/find_circle_num.rs) | [Leetcode](https://leetcode.com/problems/number-of-provinces/) | Medium |
5253
| 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 |
5354
| 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 |
5455
| 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 |
@@ -68,7 +69,9 @@ LeetCode is a website that has programming-related questions that are designed t
6869
| 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 |
6970
| 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 |
7071
| 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 |
72+
| 1167 | Minimum Cost to Connect Sticks | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/connect_sticks.rs) | [Leetcode](https://leetcode.com/problems/minimum-cost-to-connect-sticks/) | Medium |
7173
| 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 |
74+
| 1268 | Search Suggestions System | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/suggested_products.rs) | [Leetcode](https://leetcode.com/problems/search-suggestions-system/) | Medium |
7275
| 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 |
7376
| 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 |
7477
| 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 |
@@ -77,11 +80,12 @@ LeetCode is a website that has programming-related questions that are designed t
7780
| 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 |
7881
| 1570 | Dot Product of Two Sparse Vectors | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/dot_product.rs) | [Leetcode](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | Medium |
7982
| 1604 | Alert Using Same Key-Card Three or More Times in a One Hour Period | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/robinhood/alert_names.rs) | [Leetcode](https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/) | Medium |
83+
| 1710 | Maximum Units on a Truck | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/maximum_units.rs) | [Leetcode](https://leetcode.com/problems/maximum-units-on-a-truck/) | Easy |
8084
| 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 |
8185
| 1962 | Remove Stones to Minimize the Total | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_stone_sum.rs) | [Leetcode](https://leetcode.com/problems/remove-stones-to-minimize-the-total/) | Medium |
8286
| 2022 | Convert 1D Array Into 2D Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/convert-1d-array-into-2d-array/) | Easy |
8387
| 2023 | Number of Pairs of Strings With Concatenation Equal to Target | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/) | Medium |
8488
| 2024 | Maximize the Confusion of an Exam | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | Medium |
8589
| 2025 | Maximum Number of Ways to Partition an Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximum-number-of-ways-to-partition-an-array/) | Hard |
8690
| 2027 | Minimum Moves to Convert String | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/minimum-moves-to-convert-string/) | Easy |
87-
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |
91+
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::collections::BinaryHeap;
2+
3+
// 1167. Minimum Cost to Connect Sticks, Medium
4+
// https://leetcode.com/problems/minimum-cost-to-connect-sticks/
5+
impl Solution {
6+
pub fn connect_sticks(sticks: Vec<i32>) -> i32 {
7+
let sticks = sticks.iter().map(|x| -x).collect::<Vec<i32>>();
8+
let mut sticks_heap = BinaryHeap::from(sticks);
9+
10+
let mut cost = 0;
11+
while sticks_heap.len() > 1 {
12+
let stick = sticks_heap.pop().unwrap() + sticks_heap.pop().unwrap();
13+
sticks_heap.push(stick);
14+
cost += -stick;
15+
}
16+
17+
cost
18+
}
19+
}
20+
21+
struct Solution {}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
use crate::vec_vec_i32;
27+
28+
#[test]
29+
fn test_connect_sticks() {
30+
assert_eq!(Solution::connect_sticks(vec![2, 4, 3]), 14);
31+
}
32+
33+
#[test]
34+
fn test_connect_sticks2() {
35+
assert_eq!(Solution::connect_sticks(vec![1, 8, 3, 5]), 30);
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 547. Number of Provinces, Medium
2+
// https://leetcode.com/problems/number-of-provinces/
3+
impl Solution {
4+
pub fn find_circle_num(mut is_connected: Vec<Vec<i32>>) -> i32 {
5+
let n = is_connected.len();
6+
7+
let mut provinces = 0;
8+
fn bfs(i: usize, is_connected: &mut Vec<Vec<i32>>) {
9+
let edges = is_connected[i].clone();
10+
is_connected[i] = vec![];
11+
12+
for (i, is_edge) in edges.iter().enumerate() {
13+
if *is_edge == 1 {
14+
bfs(i as usize, is_connected);
15+
}
16+
}
17+
}
18+
19+
for i in 0..n {
20+
if is_connected[i].len() > 0 {
21+
provinces += 1;
22+
}
23+
bfs(i, &mut is_connected);
24+
}
25+
26+
provinces
27+
}
28+
}
29+
30+
struct Solution {}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
use crate::vec_vec_i32;
36+
37+
#[test]
38+
fn test_find_circle_num() {
39+
assert_eq!(Solution::find_circle_num(vec_vec_i32![[1, 1, 0], [1, 1, 0], [0, 0, 1]]), 2);
40+
}
41+
42+
#[test]
43+
fn test_find_circle_num2() {
44+
assert_eq!(Solution::find_circle_num(vec_vec_i32![[1, 0, 0], [0, 1, 0], [0, 0, 1]]), 3);
45+
}
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 1710. Maximum Units on a Truck, Easy
2+
// https://leetcode.com/problems/maximum-units-on-a-truck/
3+
impl Solution {
4+
pub fn maximum_units(mut box_types: Vec<Vec<i32>>, mut truck_size: i32) -> i32 {
5+
box_types.sort_unstable_by(|a, b| b[1].cmp(&a[1]));
6+
7+
let mut loaded_boxes = 0;
8+
9+
for box_type in box_types {
10+
if truck_size > box_type[0] {
11+
truck_size -= box_type[0];
12+
loaded_boxes += box_type[0] * box_type[1];
13+
} else {
14+
loaded_boxes += truck_size * box_type[1];
15+
return loaded_boxes;
16+
}
17+
}
18+
19+
loaded_boxes
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_maximum_units() {
32+
assert_eq!(Solution::maximum_units(vec_vec_i32![[1, 3], [2, 2], [3, 1]], 4), 8);
33+
}
34+
35+
#[test]
36+
fn test_maximum_units2() {
37+
assert_eq!(Solution::maximum_units(vec_vec_i32![[5, 10], [2, 5], [4, 7], [3, 9]], 10), 91);
38+
}
39+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 1268. Search Suggestions System, Medium
2+
// https://leetcode.com/problems/search-suggestions-system/
3+
impl Solution {
4+
pub fn suggested_products(products: Vec<String>, search_word: String) -> Vec<Vec<String>> {
5+
let mut suggested_products = vec![];
6+
for i in 0..search_word.len() {
7+
println!("{}", &search_word[..i + 1]);
8+
let mut filtered = products
9+
.iter()
10+
.filter(|p| p.starts_with(&search_word[..i + 1]))
11+
.map(|p| p.to_string())
12+
.collect::<Vec<String>>();
13+
filtered.sort();
14+
15+
suggested_products.push(filtered.iter().take(3).cloned().collect::<Vec<String>>());
16+
}
17+
18+
suggested_products
19+
}
20+
}
21+
22+
struct Solution {}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
use crate::{vec_string, vec_vec_string};
28+
29+
#[test]
30+
fn test_suggested_products() {
31+
assert_eq!(
32+
Solution::suggested_products(vec_string!["mobile", "mouse", "moneypot", "monitor", "mousepad"], "mouse".to_string()),
33+
vec_vec_string![
34+
["mobile", "moneypot", "monitor"],
35+
["mobile", "moneypot", "monitor"],
36+
["mouse", "mousepad"],
37+
["mouse", "mousepad"],
38+
["mouse", "mousepad"]
39+
]
40+
);
41+
}
42+
43+
#[test]
44+
fn test_suggested_products2() {
45+
assert_eq!(
46+
Solution::suggested_products(vec_string!["havana"], "havana".to_string()),
47+
vec_vec_string![["havana"], ["havana"], ["havana"], ["havana"], ["havana"], ["havana"]]
48+
);
49+
}
50+
51+
#[test]
52+
fn test_suggested_products3() {
53+
assert_eq!(
54+
Solution::suggested_products(vec_string!["bags", "baggage", "banner", "box", "cloths"], "bags".to_string()),
55+
vec_vec_string![["baggage", "bags", "banner"], ["baggage", "bags", "banner"], ["baggage", "bags"], ["bags"]]
56+
);
57+
}
58+
59+
#[test]
60+
fn test_suggested_products4() {
61+
assert_eq!(
62+
Solution::suggested_products(vec_string!["havana"], "tatiana".to_string()),
63+
vec_vec_string![[], [], [], [], [], [], []]
64+
);
65+
}
66+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /