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 431cd0a

Browse files
committed
Add solutions
1 parent 59958ab commit 431cd0a

File tree

9 files changed

+360
-4
lines changed

9 files changed

+360
-4
lines changed

‎README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LeetCode is a website that has programming-related questions that are designed t
2121
| 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 |
2222
| 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 |
2323
| 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 |
24+
| 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 |
2425
| 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 |
2526
| 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 |
2627
| 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,6 +36,7 @@ LeetCode is a website that has programming-related questions that are designed t
3536
| 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 |
3637
| 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 |
3738
| 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 |
39+
| 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 |
3840
| 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 |
3941
| 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 |
4042
| 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 |
@@ -47,4 +49,8 @@ LeetCode is a website that has programming-related questions that are designed t
4749
| 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 |
4850
| 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 |
4951
| 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 |
50-
| 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 |
52+
| 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+
| 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 |
54+
| 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 |
55+
| 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 |
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 174. Dungeon Game, Hard
2+
// https://leetcode.com/problems/dungeon-game/
3+
impl Solution {
4+
pub fn calculate_minimum_hp(dungeon: Vec<Vec<i32>>) -> i32 {
5+
let [n, m] = [dungeon.len(), dungeon[0].len()];
6+
let mut dp = vec![vec![i32::max_value(); m + 1]; n + 1]; // (curr_health, min_health)
7+
8+
dp[n - 1][m] = 1;
9+
dp[n][m - 1] = 1;
10+
11+
for i in (0..n).rev() {
12+
for j in (0..m).rev() {
13+
dp[i][j] = i32::max(i32::min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j], 1);
14+
}
15+
}
16+
17+
dp[0][0]
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_calculate_minimum_hp() {
30+
assert_eq!(Solution::calculate_minimum_hp(vec_vec_i32![[-2, -3, 3], [-5, -10, 1], [10, 30, -5]]), 7);
31+
}
32+
33+
#[test]
34+
fn test_calculate_minimum_hp2() {
35+
assert_eq!(Solution::calculate_minimum_hp(vec_vec_i32![[0]]), 1);
36+
}
37+
38+
#[test]
39+
fn test_calculate_minimum_hp3() {
40+
assert_eq!(Solution::calculate_minimum_hp(vec_vec_i32![[100]]), 1);
41+
}
42+
43+
#[test]
44+
fn test_calculate_minimum_hp4() {
45+
assert_eq!(Solution::calculate_minimum_hp(vec_vec_i32![[1, -3, 3], [0, -2, 0], [-3, -3, -3]]), 3);
46+
}
47+
}

‎src/leetcode/challenge/cherry_pickup.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::collections::HashMap;
2+
3+
// 741. Cherry Pickup, Hard
4+
// https://leetcode.com/problems/cherry-pickup/
5+
impl Solution {
6+
pub fn cherry_pickup(grid: Vec<Vec<i32>>) -> i32 {
7+
let n = grid.len();
8+
9+
let mut memo: HashMap<(usize, usize, usize, usize), i32> = HashMap::new();
10+
11+
fn dp(grid: &Vec<Vec<i32>>, n: &usize, i1: usize, j1: usize, i2: usize, j2: usize, memo: &mut HashMap<(usize, usize, usize, usize), i32>) -> i32 {
12+
if i1 >= *n || j1 >= *n || i2 >= *n || j2 >= *n {
13+
return -1;
14+
} else if i1 == *n - 1 && j1 == *n - 1 && i2 == *n - 1 && j2 == *n - 1 {
15+
return grid[i1][j1];
16+
} else if grid[i1][j1] == -1 || grid[i2][j2] == -1 {
17+
return -1;
18+
}
19+
20+
let key = &(i1, j1, i2, j2);
21+
if memo.contains_key(key) {
22+
return *memo.get(key).unwrap();
23+
}
24+
25+
let dd = dp(&grid, &n, i1 + 1, j1, i2 + 1, j2, memo);
26+
let dr = dp(&grid, &n, i1 + 1, j1, i2, j2 + 1, memo);
27+
let rd = dp(&grid, &n, i1, j1 + 1, i2 + 1, j2, memo);
28+
let rr = dp(&grid, &n, i1, j1 + 1, i2, j2 + 1, memo);
29+
30+
let comb = i32::max(dd, i32::max(dr, i32::max(rd, rr)));
31+
32+
let out;
33+
if comb == -1 {
34+
out = -1;
35+
} else {
36+
if i1 == i2 && j1 == j2 {
37+
out = comb + grid[i1][j1];
38+
} else {
39+
out = comb + grid[i1][j1] + grid[i2][j2];
40+
}
41+
}
42+
43+
memo.insert(*key, out);
44+
out
45+
}
46+
47+
i32::max(dp(&grid, &n, 0, 0, 0, 0, &mut memo), 0)
48+
}
49+
}
50+
51+
struct Solution {}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
use crate::vec_vec_i32;
57+
58+
#[test]
59+
fn test_cherry_pickup() {
60+
assert_eq!(Solution::cherry_pickup(vec_vec_i32![[0, 1, -1], [1, 0, -1], [1, 1, 1]]), 5);
61+
}
62+
63+
#[test]
64+
fn test_cherry_pickup2() {
65+
assert_eq!(Solution::cherry_pickup(vec_vec_i32![[1, 1, -1], [1, -1, 1], [-1, 1, 1]]), 0);
66+
}
67+
}

‎src/leetcode/challenge/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod break_palindrome;
2+
mod calculate_minimum_hp;
23
mod can_partition_k_subsets;
4+
mod cherry_pickup;
35
mod find_max_consecutive_ones;
46
mod intersect;
57
mod longest_common_subsequence;

‎src/leetcode/contest/biweekly_62.rs

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
use std::collections::HashMap;
2+
3+
// 2022. Convert 1D Array Into 2D Array, Easy
4+
// https://leetcode.com/problems/convert-1d-array-into-2d-array/
5+
impl Solution1 {
6+
pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
7+
// check space
8+
if original.len() != m as usize * n as usize {
9+
return vec![];
10+
}
11+
12+
let mut ans = vec![vec![0; n as usize]; m as usize];
13+
let mut idx = 0;
14+
for a in original {
15+
ans[idx / n as usize][idx % n as usize] = a;
16+
idx += 1;
17+
}
18+
19+
ans
20+
}
21+
}
22+
23+
// 2023. Number of Pairs of Strings With Concatenation Equal to Target, Medium
24+
// https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/
25+
impl Solution2 {
26+
pub fn num_of_pairs(nums: Vec<String>, target: String) -> i32 {
27+
let mut ans = 0;
28+
for i in 0..nums.len() {
29+
for j in 0..nums.len() {
30+
if i != j {
31+
if [nums[i].clone(), nums[j].clone()].concat() == target {
32+
ans += 1;
33+
}
34+
}
35+
}
36+
}
37+
38+
ans as i32
39+
}
40+
}
41+
42+
// 2024. Maximize the Confusion of an Exam, Medium
43+
// https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
44+
impl Solution3 {
45+
pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 {
46+
let nums: Vec<i32> = answer_key.chars().map(|c| if c == 'F' { 1 } else { 0 }).collect();
47+
48+
let [mut l, mut r] = [0, 0];
49+
let mut uses = 0;
50+
let n = nums.len();
51+
let mut ans = 0;
52+
53+
while r < n {
54+
if nums[r] == 0 {
55+
uses += 1;
56+
}
57+
while uses > k {
58+
if nums[l] == 0 {
59+
uses -= 1;
60+
}
61+
l += 1;
62+
}
63+
64+
ans = ans.max(r - l);
65+
r += 1;
66+
}
67+
68+
r = 0;
69+
l = 0;
70+
uses = 0;
71+
72+
while r < n {
73+
if nums[r] == 1 {
74+
uses += 1;
75+
}
76+
while uses > k {
77+
if nums[l] == 1 {
78+
uses -= 1;
79+
}
80+
l += 1;
81+
}
82+
83+
ans = ans.max(r - l);
84+
r += 1;
85+
}
86+
87+
ans as i32 + 1
88+
}
89+
}
90+
91+
// 2025. Maximum Number of Ways to Partition an Array, Hard
92+
// https://leetcode.com/problems/maximum-number-of-ways-to-partition-an-array/
93+
impl Solution4 {
94+
pub fn ways_to_partition(nums: Vec<i32>, k: i32) -> i32 {
95+
let mut pre_calcs = Vec::<(i32, i32, usize)>::new();
96+
97+
let mut part1 = 0;
98+
let mut part2 = nums.iter().sum::<i32>();
99+
for i in 0..nums.len() {
100+
part1 = part1 + nums[i];
101+
part2 = part2 - nums[i];
102+
103+
pre_calcs.push((part1, part2, i));
104+
}
105+
106+
let mut max = 0;
107+
for i in 0..nums.len() {
108+
let mut curr = 0;
109+
for pre_calc in pre_calcs.iter() {
110+
if i <= pre_calc.2 && pre_calc.0 - nums[i] + k == pre_calc.1 {
111+
curr += 1;
112+
} else if i >= pre_calc.2 && pre_calc.0 == pre_calc.1 - nums[i] + k {
113+
curr += 1;
114+
} else if pre_calc.0 == pre_calc.1 {
115+
curr += 1;
116+
}
117+
}
118+
119+
max = max.max(curr - 1);
120+
}
121+
122+
max
123+
}
124+
}
125+
126+
struct Solution1 {}
127+
struct Solution2 {}
128+
struct Solution3 {}
129+
struct Solution4 {}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use super::*;
134+
use crate::{vec_string, vec_vec_i32};
135+
136+
#[test]
137+
fn test_construct2_d_array() {
138+
assert_eq!(Solution1::construct2_d_array(vec![1, 2, 3, 4], 2, 2), vec_vec_i32![[1, 2], [3, 4]]);
139+
}
140+
141+
#[test]
142+
fn test_construct2_d_array2() {
143+
assert_eq!(Solution1::construct2_d_array(vec![1, 2, 3], 1, 3), vec_vec_i32![[1, 2, 3]]);
144+
}
145+
146+
#[test]
147+
fn test_construct2_d_array3() {
148+
assert_eq!(Solution1::construct2_d_array(vec![1, 2], 1, 1), vec_vec_i32![]);
149+
}
150+
151+
#[test]
152+
fn test_construct2_d_array4() {
153+
assert_eq!(Solution1::construct2_d_array(vec![3], 1, 2), vec_vec_i32![]);
154+
}
155+
156+
#[test]
157+
fn test_num_of_pairs() {
158+
assert_eq!(Solution2::num_of_pairs(vec_string!["777", "7", "77", "77"], "7777".to_string()), 4);
159+
}
160+
161+
#[test]
162+
fn test_num_of_pairs2() {
163+
assert_eq!(Solution2::num_of_pairs(vec_string!["123", "4", "12", "34"], "1234".to_string()), 2);
164+
}
165+
166+
#[test]
167+
fn test_num_of_pairs3() {
168+
assert_eq!(Solution2::num_of_pairs(vec_string!["1", "1", "1"], "11".to_string()), 6);
169+
}
170+
171+
#[test]
172+
fn test_max_consecutive_answers() {
173+
assert_eq!(Solution3::max_consecutive_answers("TTFF".to_string(), 2), 4);
174+
}
175+
176+
#[test]
177+
fn test_max_consecutive_answers2() {
178+
assert_eq!(Solution3::max_consecutive_answers("TFFT".to_string(), 1), 3);
179+
}
180+
181+
#[test]
182+
fn test_max_consecutive_answers3() {
183+
assert_eq!(Solution3::max_consecutive_answers("TTFTTFTT".to_string(), 1), 5);
184+
}
185+
186+
#[test]
187+
fn test_max_consecutive_answers4() {
188+
assert_eq!(
189+
Solution3::max_consecutive_answers(
190+
"FTFFTFTFTTFTTFTTFFTTFFTTTTTFTTTFTFFTTFFFFFTTTTFTTTTTTTTTFTTFFTTFTFFTTTFFFFFTTTFFTTTTFTFTFFTTFTTTTTTF".to_string(),
191+
32
192+
),
193+
85
194+
);
195+
}
196+
197+
#[test]
198+
fn test_ways_to_partition() {
199+
assert_eq!(Solution4::ways_to_partition(vec![2, -1, 2], 3), 1);
200+
}
201+
202+
#[test]
203+
fn test_ways_to_partition2() {
204+
assert_eq!(Solution4::ways_to_partition(vec![0, 0, 0], 1), 2);
205+
}
206+
207+
#[test]
208+
fn test_ways_to_partition4() {
209+
assert_eq!(
210+
Solution4::ways_to_partition(
211+
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30827, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
212+
0
213+
),
214+
33
215+
);
216+
}
217+
218+
#[test]
219+
fn test_ways_to_partition5() {
220+
assert_eq!(
221+
Solution4::ways_to_partition(
222+
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -93166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
223+
69428
224+
),
225+
37
226+
);
227+
}
228+
}

‎src/leetcode/contest/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod biweekly_62;
2+
mod weekly_261;

‎src/leetcode/contest/weekly_261.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎src/leetcode/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod challenge;
2+
mod contest;
23
mod problem;
34
mod top_interview;
45
#[cfg_attr(test, macro_use)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /