Skip to content

Commit 1e5234a

Browse files
committed
Add two solutions
1 parent 63bb31f commit 1e5234a

File tree

4 files changed

+99
-21
lines changed

4 files changed

+99
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
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 (80)
9+
## Solutions (82)
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+
| 516 | Longest Palindromic Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/longest_palindrome_subseq.rs) | [Leetcode](https://leetcode.com/problems/longest-palindromic-subsequence/) | Medium |
5253
| 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 |
5354
| 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 |
5455
| 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 |
@@ -70,6 +71,7 @@ LeetCode is a website that has programming-related questions that are designed t
7071
| 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 |
7172
| 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 |
7273
| 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 |
74+
| 1216 | Valid Palindrome III | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/is_valid_palindrome.rs) | [Leetcode](https://leetcode.com/problems/valid-palindrome-iii/) | Hard |
7375
| 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 |
7476
| 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 |
7577
| 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 |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 1216. Valid Palindrome III, Hard
2+
// https://leetcode.com/problems/valid-palindrome-iii/
3+
impl Solution {
4+
pub fn is_valid_palindrome(s: String, k: i32) -> bool {
5+
let n = s.len();
6+
let mut dp = vec![vec![0; n]; n];
7+
8+
let chars = s.chars().collect::<Vec<char>>();
9+
10+
for i in 0..n {
11+
dp[i][i] = 1;
12+
}
13+
14+
for i in 1..n {
15+
for j in 0..n - i {
16+
let i = j + i;
17+
if chars[j] == chars[i] {
18+
dp[j][i] = 2 + dp[j + 1][i - 1];
19+
} else {
20+
dp[j][i] = i32::max(dp[j + 1][i], dp[j][i - 1]);
21+
}
22+
}
23+
}
24+
25+
dp[0][n - 1] >= n as i32 - k
26+
}
27+
}
28+
29+
struct Solution {}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
use crate::{vec_vec_char, vec_vec_i32};
35+
36+
#[test]
37+
fn test_is_valid_palindrome() {
38+
assert_eq!(Solution::is_valid_palindrome("abcdeca".to_string(), 2), true);
39+
}
40+
41+
#[test]
42+
fn test_is_valid_palindrome2() {
43+
assert_eq!(Solution::is_valid_palindrome("abbababa".to_string(), 1), true);
44+
}
45+
}

src/leetcode/challenge/mod.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1 @@
1-
mod break_palindrome;
2-
mod calculate_minimum_hp;
3-
mod can_partition_k_subsets;
4-
mod cherry_pickup;
5-
mod find_max_consecutive_ones;
6-
mod intersect;
7-
mod longest_common_subsequence;
8-
mod max_length;
9-
mod max_sub_array_len;
10-
mod minmax_gas_dist;
11-
mod moves_to_chessboard;
12-
mod num_distinct;
13-
mod num_unique_emails;
14-
mod shortest_distance;
15-
mod shortest_path;
16-
mod sort_array_by_parity_ii;
17-
mod spiral_order;
18-
mod split_list_to_parts;
19-
mod tictactoe;
20-
mod tribonacci;
1+
automod::dir!("src/leetcode/challenge");
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 516. Longest Palindromic Subsequence, Medium
2+
// https://leetcode.com/problems/longest-palindromic-subsequence/
3+
impl Solution {
4+
pub fn longest_palindrome_subseq(s: String) -> i32 {
5+
let n = s.len();
6+
let mut dp = vec![vec![0; n]; n];
7+
8+
let chars = s.chars().collect::<Vec<char>>();
9+
10+
for i in 0..n {
11+
dp[i][i] = 1;
12+
}
13+
14+
for i in 1..n {
15+
for j in 0..s.len() - i {
16+
let i = j + i;
17+
if chars[j] == chars[i] {
18+
dp[j][i] = 2 + dp[j + 1][i - 1];
19+
} else {
20+
dp[j][i] = i32::max(dp[j + 1][i], dp[j][i - 1]);
21+
}
22+
}
23+
}
24+
25+
dp[0][n - 1]
26+
}
27+
}
28+
29+
struct Solution {}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
use crate::{vec_vec_char, vec_vec_i32};
35+
36+
#[test]
37+
fn test_longest_palindrome_subseq() {
38+
assert_eq!(Solution::longest_palindrome_subseq("bbbab".to_string()), 4);
39+
}
40+
41+
#[test]
42+
fn test_longest_palindrome_subseq2() {
43+
assert_eq!(Solution::longest_palindrome_subseq("cbbd".to_string()), 2);
44+
}
45+
46+
#[test]
47+
fn test_longest_palindrome_subseq3() {
48+
assert_eq!(Solution::longest_palindrome_subseq("cbbd".to_string()), 2);
49+
}
50+
}

0 commit comments

Comments
 (0)