Skip to content

Commit 1d3cc9a

Browse files
committed
Add more solutions
1 parent 431cd0a commit 1d3cc9a

File tree

6 files changed

+327
-2
lines changed

6 files changed

+327
-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
9+
## Solutions (49)
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 |
@@ -50,7 +50,11 @@ LeetCode is a website that has programming-related questions that are designed t
5050
| 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 |
5151
| 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 |
5252
| 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 |
53+
| 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 |
54+
| 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 |
5355
| 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 |
5456
| 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 |
5557
| 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 |
56-
| 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 |
58+
| 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 |
59+
| 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 |
60+
| 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 |

src/leetcode/contest/weekly_261.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,138 @@
1+
use std::collections::HashMap;
12

3+
// 2027. Minimum Moves to Convert String, Easy
4+
// https://leetcode.com/problems/minimum-moves-to-convert-string/
5+
impl Solution1 {
6+
pub fn minimum_moves(s: String) -> i32 {
7+
let mut arr: Vec<i32> = vec![];
8+
9+
for ch in s.chars() {
10+
if ch == 'X' {
11+
arr.push(1);
12+
} else {
13+
arr.push(0);
14+
}
15+
}
16+
17+
let mut moves = 0;
18+
let n = arr.len();
19+
let [mut l, mut r] = [0, 0];
20+
while r < n {
21+
if r - l < 2 {
22+
r += 1;
23+
} else {
24+
if arr[l] == 1 {
25+
arr[l] = 0;
26+
arr[l + 1] = 0;
27+
arr[l + 2] = 0;
28+
moves += 1;
29+
} else {
30+
l += 1;
31+
}
32+
}
33+
34+
if r == n && (arr[r - 1] == 1 || arr[r - 2] == 1 || arr[r - 3] == 1) {
35+
moves += 1;
36+
}
37+
}
38+
39+
moves
40+
}
41+
}
42+
43+
// 2028. Find Missing Observations, Medium
44+
// https://leetcode.com/problems/find-missing-observations/
45+
impl Solution2 {
46+
pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
47+
if rolls.len() == 0 || n == 0 {
48+
return vec![];
49+
}
50+
51+
let m = rolls.len() as i32;
52+
53+
let have_sum = rolls.iter().sum::<i32>();
54+
let mut missing_sum = (mean * (n + m)) - have_sum;
55+
56+
let avg_missing = missing_sum / n;
57+
let mut action = true;
58+
if avg_missing > 0 && avg_missing <= 6 {
59+
let mut missing_rolls = vec![avg_missing; n as usize];
60+
missing_sum -= avg_missing * n;
61+
'outer: while missing_sum > 0 {
62+
for i in 0..n {
63+
if missing_sum <= 0 || !action {
64+
if !action {
65+
return vec![];
66+
}
67+
break 'outer;
68+
}
69+
if missing_rolls[i as usize] < 6 {
70+
let add = i32::min(6 - missing_rolls[i as usize], missing_sum);
71+
missing_rolls[i as usize] += add;
72+
missing_sum -= add;
73+
action = true;
74+
} else {
75+
action = false;
76+
}
77+
}
78+
}
79+
80+
return missing_rolls;
81+
} else {
82+
return vec![];
83+
}
84+
}
85+
}
86+
87+
struct Solution1 {}
88+
struct Solution2 {}
89+
#[cfg(test)]
90+
mod tests {
91+
use super::*;
92+
use crate::{vec_string, vec_vec_i32};
93+
94+
#[test]
95+
fn test_minimum_moves() {
96+
assert_eq!(Solution1::minimum_moves("XXX".to_string()), 1);
97+
}
98+
99+
#[test]
100+
fn test_minimum_moves2() {
101+
assert_eq!(Solution1::minimum_moves("XXOX".to_string()), 2);
102+
}
103+
104+
#[test]
105+
fn test_minimum_moves3() {
106+
assert_eq!(Solution1::minimum_moves("OOOO".to_string()), 0);
107+
}
108+
109+
#[test]
110+
fn test_missing_rolls() {
111+
assert_eq!(Solution2::missing_rolls(vec![3, 2, 4, 3], 4, 2), vec![6, 6]);
112+
}
113+
114+
#[test]
115+
fn test_missing_rolls2() {
116+
assert_eq!(Solution2::missing_rolls(vec![1, 5, 6], 3, 4), vec![3, 2, 2, 2]);
117+
}
118+
119+
#[test]
120+
fn test_missing_rolls3() {
121+
assert_eq!(Solution2::missing_rolls(vec![1, 2, 3, 4], 6, 4), vec![]);
122+
}
123+
124+
#[test]
125+
fn test_missing_rolls4() {
126+
assert_eq!(Solution2::missing_rolls(vec![1], 3, 1), vec![5]);
127+
}
128+
129+
#[test]
130+
fn test_missing_rolls5() {
131+
assert_eq!(Solution2::missing_rolls(vec![3, 5, 4], 5, 3), vec![6, 6, 6]);
132+
}
133+
134+
#[test]
135+
fn test_missing_rolls6() {
136+
assert_eq!(Solution2::missing_rolls(vec![3, 5, 3], 5, 3), vec![]);
137+
}
138+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use std::collections::BinaryHeap;
2+
3+
// 1801. Number of Orders in the Backlog, Medium
4+
// https://leetcode.com/problems/number-of-orders-in-the-backlog/
5+
impl Solution {
6+
pub fn get_number_of_backlog_orders(orders: Vec<Vec<i32>>) -> i32 {
7+
#[derive(Eq, Debug, Clone)]
8+
struct Order {
9+
price: i32,
10+
amount: i32,
11+
order_type: i32,
12+
}
13+
14+
impl Order {
15+
fn new(mut price: i32, amount: i32, order_type: i32) -> Self {
16+
if order_type == 1 {
17+
price *= -1;
18+
}
19+
20+
Self { price, amount, order_type }
21+
}
22+
}
23+
24+
impl Ord for Order {
25+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
26+
self.price.cmp(&other.price)
27+
}
28+
}
29+
30+
impl PartialOrd for Order {
31+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
32+
Some(self.cmp(other))
33+
}
34+
}
35+
36+
impl PartialEq for Order {
37+
fn eq(&self, other: &Self) -> bool {
38+
self.price == other.price
39+
}
40+
}
41+
42+
let base: i32 = 10;
43+
let modulo: f64 = (base.pow(9) + 7) as f64;
44+
45+
let mut buy_backlog: BinaryHeap<Order> = BinaryHeap::new();
46+
let mut sell_backlog: BinaryHeap<Order> = BinaryHeap::new();
47+
48+
for order in orders.iter() {
49+
let mut o = Order::new(order[0], order[1], order[2]);
50+
51+
while o.amount > 0 {
52+
if o.order_type == 0 {
53+
if !sell_backlog.is_empty() && sell_backlog.peek().unwrap().price * -1 <= o.price {
54+
let mut sell_order = sell_backlog.pop().unwrap();
55+
let amount = i32::min(sell_order.amount, o.amount);
56+
o.amount -= amount;
57+
sell_order.amount -= amount;
58+
59+
if sell_order.amount > 0 {
60+
sell_backlog.push(sell_order);
61+
}
62+
} else {
63+
buy_backlog.push(o.clone());
64+
break;
65+
}
66+
} else {
67+
if !buy_backlog.is_empty() && buy_backlog.peek().unwrap().price >= o.price * -1 {
68+
let mut buy_order = buy_backlog.pop().unwrap();
69+
let amount = i32::min(buy_order.amount, o.amount);
70+
o.amount -= amount;
71+
buy_order.amount -= amount;
72+
73+
if buy_order.amount > 0 {
74+
buy_backlog.push(buy_order);
75+
}
76+
} else {
77+
sell_backlog.push(o.clone());
78+
break;
79+
}
80+
}
81+
}
82+
}
83+
84+
((buy_backlog.iter().map(|order| order.amount as f64).sum::<f64>() + sell_backlog.iter().map(|order| order.amount as f64).sum::<f64>()) % modulo) as i32
85+
}
86+
}
87+
88+
struct Solution {}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use super::*;
93+
use crate::vec_vec_i32;
94+
95+
#[test]
96+
fn test_get_number_of_backlog_orders() {
97+
assert_eq!(Solution::get_number_of_backlog_orders(vec_vec_i32![[10, 5, 0], [15, 2, 1], [25, 1, 1], [30, 4, 0]]), 6);
98+
}
99+
100+
#[test]
101+
fn test_get_number_of_backlog_orders2() {
102+
assert_eq!(
103+
Solution::get_number_of_backlog_orders(vec_vec_i32![[7, 1000000000, 1], [15, 3, 0], [5, 999999995, 0], [5, 1, 1]]),
104+
999999984
105+
);
106+
}
107+
108+
#[test]
109+
fn test_get_number_of_backlog_orders3() {
110+
assert_eq!(Solution::get_number_of_backlog_orders(vec_vec_i32![[19, 28, 0], [9, 4, 1], [25, 15, 1]]), 39);
111+
}
112+
113+
#[test]
114+
fn test_get_number_of_backlog_orders4() {
115+
assert_eq!(
116+
Solution::get_number_of_backlog_orders(vec_vec_i32![
117+
[944925198, 885003661, 0],
118+
[852263791, 981056352, 0],
119+
[16300530, 415829909, 0],
120+
[940927944, 713835606, 0],
121+
[606389372, 407474168, 1],
122+
[139563740, 85382287, 1],
123+
[700244880, 901922025, 1],
124+
[972900669, 15506445, 0],
125+
[576578542, 65339074, 0],
126+
[45972021, 293765308, 0],
127+
[464403992, 97750995, 0],
128+
[29659852, 536508041, 0],
129+
[799523481, 299864737, 0],
130+
[711908211, 480514887, 1],
131+
[354125407, 677598767, 1],
132+
[279004011, 688916331, 0],
133+
[263524013, 64622178, 0],
134+
[375395974, 460070320, 0],
135+
[971786816, 379275200, 1],
136+
[577774472, 214070125, 1],
137+
[987757349, 711231195, 0]
138+
]),
139+
83062672
140+
);
141+
}
142+
}

src/leetcode/problem/min_stone_sum.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::collections::BinaryHeap;
2+
3+
// 1962. Remove Stones to Minimize the Total, Medium
4+
// https://leetcode.com/problems/remove-stones-to-minimize-the-total/
5+
impl Solution {
6+
pub fn min_stone_sum(piles: Vec<i32>, k: i32) -> i32 {
7+
let mut heap = BinaryHeap::from(piles);
8+
9+
for _ in 0..k {
10+
let mut t = heap.pop().unwrap_or(0);
11+
t -= f32::floor(t as f32 / 2.0) as i32;
12+
heap.push(t);
13+
}
14+
15+
heap.iter().sum()
16+
}
17+
}
18+
19+
struct Solution {}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
use crate::vec_vec_i32;
25+
26+
#[test]
27+
fn test_min_stone_sum() {
28+
assert_eq!(Solution::min_stone_sum(vec![5, 4, 9], 2), 12);
29+
}
30+
31+
#[test]
32+
fn test_min_stone_sum2() {
33+
assert_eq!(Solution::min_stone_sum(vec![4, 3, 6, 7], 3), 12);
34+
}
35+
}

src/leetcode/problem/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ mod add_operators;
22
mod erase_overlap_intervals;
33
mod find_min_arrow_shots;
44
mod find_poisoned_duration;
5+
mod get_number_of_backlog_orders;
56
mod is_anagram;
67
mod is_good_array;
78
mod largest_divisible_subset;
89
mod max_uncrossed_lines;
910
mod min_distance;
1011
mod min_eating_speed;
12+
mod min_stone_sum;
1113
mod num_subarray_product_less_than_k;
1214
mod pseudo_palindromic_paths;
1315
mod two_sum;

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ fn gen_table() -> Result<()> {
5353
}
5454

5555
entries.sort();
56+
println!("## Solutions ({}) ", entries.len());
57+
println!(
58+
r#"| No. | Title | Solution | Problem | Difficulty |
59+
|:---:|:------|:--------:|:-------:|:----------:|"#
60+
);
5661
for entry in entries {
5762
println!(
5863
"| {} | {} | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/{}) | [Leetcode]({}) | {} |",

0 commit comments

Comments
 (0)