Skip to content

Commit 793e06d

Browse files
committed
Add more solutions
1 parent 9ec32b9 commit 793e06d

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-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 (105)
9+
## Solutions (109)
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 |
@@ -15,7 +15,7 @@ LeetCode is a website that has programming-related questions that are designed t
1515
| 5 | Longest Palindromic Substring | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/longest_palindrome.rs) | [Leetcode](https://leetcode.com/problems/longest-palindromic-substring/) | Medium |
1616
| 31 | Next Permutation | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/next_permutation.rs) | [Leetcode](https://leetcode.com/problems/next-permutation/) | Medium |
1717
| 33 | Search in Rotated Sorted Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/search.rs) | [Leetcode](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium |
18-
| 34 | Find First and Last Position of Element in Sorted Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find-first-and-last-position-of-element-in-sorted-array.rs) | [Leetcode](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | Medium |
18+
| 34 | Find First and Last Position of Element in Sorted Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/search_range.rs) | [Leetcode](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | Medium |
1919
| 37 | Sudoku Solver | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/solve_sudoku.rs) | [Leetcode](https://leetcode.com/problems/sudoku-solver/) | Hard |
2020
| 42 | Trapping Rain Water | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/trap.rs) | [Leetcode](https://leetcode.com/problems/trapping-rain-water/) | Hard |
2121
| 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 |
@@ -33,6 +33,7 @@ LeetCode is a website that has programming-related questions that are designed t
3333
| 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 |
3434
| 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 |
3535
| 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 |
36+
| 123 | Best Time to Buy and Sell Stock III | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_profit2.rs) | [Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) | Hard |
3637
| 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 |
3738
| 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 |
3839
| 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 |
@@ -43,7 +44,10 @@ LeetCode is a website that has programming-related questions that are designed t
4344
| 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 |
4445
| 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 |
4546
| 253 | Meeting Rooms II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_meeting_rooms.rs) | [Leetcode](https://leetcode.com/problems/meeting-rooms-ii/) | Medium |
47+
| 279 | Perfect Squares | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/perfect_squares.rs) | [Leetcode](https://leetcode.com/problems/perfect-squares/) | Medium |
4648
| 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 |
49+
| 302 | Smallest Rectangle Enclosing Black Pixels | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_area.rs) | [Leetcode](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) | Hard |
50+
| 309 | Best Time to Buy and Sell Stock with Cooldown | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_profit.rs) | [Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | Medium |
4751
| 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 |
4852
| 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 |
4953
| 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 |

src/leetcode/problem/max_profit.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 309. Best Time to Buy and Sell Stock with Cooldown, Medium
2+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
3+
impl Solution {
4+
pub fn max_profit(prices: Vec<i32>) -> i32 {
5+
let n = prices.len();
6+
if n < 2 {
7+
return 0;
8+
}
9+
10+
let [mut sold, mut held, mut reset] = [i32::MIN, i32::MIN, 0];
11+
12+
for price in prices.iter() {
13+
let pre_sold = sold;
14+
sold = held + price;
15+
held = i32::max(held, reset - price);
16+
reset = i32::max(reset, pre_sold)
17+
}
18+
19+
i32::max(sold, reset)
20+
}
21+
}
22+
23+
struct Solution {}

src/leetcode/problem/max_profit2.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 123. Best Time to Buy and Sell Stock III, Hard
2+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
3+
impl Solution {
4+
pub fn max_profit(prices: Vec<i32>) -> i32 {
5+
let n = prices.len();
6+
if n < 2 {
7+
return 0;
8+
}
9+
10+
let [mut t1_cost, mut t2_cost] = [i32::MAX, i32::MAX];
11+
let [mut t1_profit, mut t2_profit] = [0, 0];
12+
13+
for &price in prices.iter() {
14+
t1_cost = i32::min(t1_cost, price);
15+
t1_profit = i32::max(t1_profit, price - t1_cost);
16+
t2_cost = i32::min(t2_cost, price - t1_profit);
17+
t2_profit = i32::max(t2_profit, price - t2_cost);
18+
}
19+
20+
t2_profit
21+
}
22+
}
23+
24+
struct Solution {}

src/leetcode/problem/min_area.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 302. Smallest Rectangle Enclosing Black Pixels, Hard
2+
// https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/
3+
impl Solution {
4+
pub fn min_area(image: Vec<Vec<char>>, x: i32, y: i32) -> i32 {
5+
let [mut min_x, mut max_x, mut min_y, mut max_y] = [i32::MAX, i32::MIN, i32::MAX, i32::MIN];
6+
7+
fn bfs(image: &mut Vec<Vec<char>>, x: i32, y: i32, min_x: &mut i32, max_x: &mut i32, min_y: &mut i32, max_y: &mut i32) {
8+
let [n, m] = [image.len() as i32, image[0].len() as i32];
9+
10+
if x < 0 || x >= n || y < 0 || y >= m {
11+
return;
12+
}
13+
14+
let directions = vec![(0, 1), (0, -1), (1, 0), (-1, 0)];
15+
16+
if image[x as usize][y as usize] == '1' {
17+
*min_x = i32::min(*min_x, x);
18+
*max_x = i32::max(*max_x, x);
19+
*min_y = i32::min(*min_y, y);
20+
*max_y = i32::max(*max_y, y);
21+
22+
image[x as usize][y as usize] = '0';
23+
24+
for &(dx, dy) in directions.iter() {
25+
bfs(image, x + dx, y + dy, min_x, max_x, min_y, max_y);
26+
}
27+
}
28+
}
29+
30+
let mut image = image;
31+
bfs(&mut image, x, y, &mut min_x, &mut max_x, &mut min_y, &mut max_y);
32+
33+
(max_x - min_x + 1) * (max_y - min_y + 1) as i32
34+
}
35+
}
36+
37+
struct Solution {}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
use crate::vec_vec_char;
43+
44+
#[test]
45+
fn test_island_perimeter() {
46+
assert_eq!(Solution::min_area(vec_vec_char![['0', '0', '1', '0'], ['0', '1', '1', '0'], ['0', '1', '0', '0']], 0, 2), 6);
47+
}
48+
}

0 commit comments

Comments
 (0)