Skip to content

Commit 7245e6e

Browse files
authored
Merge pull request #25 from jtr109/jump-game-ii
Jump game ii
2 parents dc4d833 + 03f7f6f commit 7245e6e

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ members = [
1717
"prefix_and_suffix_search",
1818
"non_decreasing_array",
1919
"jump_game_ii",
20+
"convert_sorted_list_to_binary_search_tree",
21+
"delete_operation_for_two_strings",
2022
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "convert_sorted_list_to_binary_search_tree"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::cell::RefCell;
2+
use std::rc::Rc;
3+
4+
// Definition for singly-linked list.
5+
#[derive(PartialEq, Eq, Clone, Debug)]
6+
pub struct ListNode {
7+
pub val: i32,
8+
pub next: Option<Box<ListNode>>,
9+
}
10+
11+
impl ListNode {
12+
#[inline]
13+
fn new(val: i32) -> Self {
14+
ListNode { next: None, val }
15+
}
16+
}
17+
18+
// Definition for a binary tree node.
19+
#[derive(Debug, PartialEq, Eq)]
20+
pub struct TreeNode {
21+
pub val: i32,
22+
pub left: Option<Rc<RefCell<TreeNode>>>,
23+
pub right: Option<Rc<RefCell<TreeNode>>>,
24+
}
25+
26+
impl TreeNode {
27+
#[inline]
28+
pub fn new(val: i32) -> Self {
29+
TreeNode {
30+
val,
31+
left: None,
32+
right: None,
33+
}
34+
}
35+
}
36+
37+
struct Solution {}
38+
39+
impl Solution {
40+
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
41+
None
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
mod test {
47+
use super::*;
48+
49+
#[test]
50+
fn example_1() {
51+
let head = vec![-10, -3, 0, 5, 9];
52+
let expected = vec![Some(0), Some(-3), Some(9), Some(-10), None, Some(5)];
53+
}
54+
55+
#[test]
56+
fn example_2() {
57+
let head: Vec<i32> = vec![];
58+
let expected: Vec<i32> = vec![];
59+
}
60+
61+
#[test]
62+
fn example_3() {
63+
let head = vec![0];
64+
let expected = vec![0];
65+
}
66+
67+
#[test]
68+
fn example_4() {
69+
let head = vec![1, 3];
70+
let expected = vec![3, 1];
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "delete_operation_for_two_strings"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3734/
3+
* https://leetcode.com/problems/delete-operation-for-two-strings/
4+
*/
5+
6+
#[cfg(test)]
7+
mod tests {
8+
#[test]
9+
fn it_works() {
10+
assert_eq!(2 + 2, 4);
11+
}
12+
}
13+
pub struct Solution {}
14+
15+
impl Solution {
16+
pub fn min_distance(word1: String, word2: String) -> i32 {
17+
let mut dp = vec![vec![None; word2.len() + 1]; word1.len() + 1];
18+
let common_length = Self::lcs(&word1, &word2, word1.len(), word2.len(), &mut dp);
19+
(word1.len() + word2.len() - 2 * common_length) as i32
20+
}
21+
22+
fn lcs(s1: &str, s2: &str, m: usize, n: usize, dp: &mut Vec<Vec<Option<usize>>>) -> usize {
23+
// comparing first m characters in s1 and first n characters in s2
24+
if let Some(length) = dp[m][n] {
25+
return length;
26+
}
27+
let length = if m == 0 || n == 0 {
28+
0
29+
} else if s1.chars().nth(m - 1) == s2.chars().nth(n - 1) {
30+
1 + Self::lcs(s1, s2, m - 1, n - 1, dp)
31+
} else {
32+
Self::lcs(s1, s2, m - 1, n, dp).max(Self::lcs(s1, s2, m, n - 1, dp))
33+
};
34+
dp[m][n] = Some(length);
35+
length
36+
}
37+
}
38+
39+
#[cfg(test)]
40+
mod test {
41+
use super::*;
42+
43+
#[test]
44+
fn example_1_lcs() {
45+
let word1 = "sea";
46+
let word2 = "eat";
47+
let expected = 2;
48+
let mut dp = vec![vec![None; word2.len() + 1]; word1.len() + 1];
49+
assert_eq!(
50+
Solution::lcs(word1, word2, word1.len(), word2.len(), &mut dp),
51+
expected
52+
);
53+
}
54+
55+
#[test]
56+
fn example_1() {
57+
let word1 = "sea";
58+
let word2 = "eat";
59+
let expected = 2;
60+
assert_eq!(
61+
Solution::min_distance(word1.to_string(), word2.to_string()),
62+
expected
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)