Skip to content

Jump game ii #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -17,4 +17,6 @@ members = [
"prefix_and_suffix_search",
"non_decreasing_array",
"jump_game_ii",
"convert_sorted_list_to_binary_search_tree",
"delete_operation_for_two_strings",
]
9 changes: 9 additions & 0 deletions convert_sorted_list_to_binary_search_tree/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "convert_sorted_list_to_binary_search_tree"
version = "0.1.0"
authors = ["Ryan Li <conbas2019@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
72 changes: 72 additions & 0 deletions convert_sorted_list_to_binary_search_tree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::cell::RefCell;
use std::rc::Rc;

// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}

impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}

// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}

struct Solution {}

impl Solution {
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
None
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn example_1() {
let head = vec![-10, -3, 0, 5, 9];
let expected = vec![Some(0), Some(-3), Some(9), Some(-10), None, Some(5)];
}

#[test]
fn example_2() {
let head: Vec<i32> = vec![];
let expected: Vec<i32> = vec![];
}

#[test]
fn example_3() {
let head = vec![0];
let expected = vec![0];
}

#[test]
fn example_4() {
let head = vec![1, 3];
let expected = vec![3, 1];
}
}
9 changes: 9 additions & 0 deletions delete_operation_for_two_strings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "delete_operation_for_two_strings"
version = "0.1.0"
authors = ["Ryan Li <conbas2019@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
65 changes: 65 additions & 0 deletions delete_operation_for_two_strings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3734/
* https://leetcode.com/problems/delete-operation-for-two-strings/
*/

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub struct Solution {}

impl Solution {
pub fn min_distance(word1: String, word2: String) -> i32 {
let mut dp = vec![vec![None; word2.len() + 1]; word1.len() + 1];
let common_length = Self::lcs(&word1, &word2, word1.len(), word2.len(), &mut dp);
(word1.len() + word2.len() - 2 * common_length) as i32
}

fn lcs(s1: &str, s2: &str, m: usize, n: usize, dp: &mut Vec<Vec<Option<usize>>>) -> usize {
// comparing first m characters in s1 and first n characters in s2
if let Some(length) = dp[m][n] {
return length;
}
let length = if m == 0 || n == 0 {
0
} else if s1.chars().nth(m - 1) == s2.chars().nth(n - 1) {
1 + Self::lcs(s1, s2, m - 1, n - 1, dp)
} else {
Self::lcs(s1, s2, m - 1, n, dp).max(Self::lcs(s1, s2, m, n - 1, dp))
};
dp[m][n] = Some(length);
length
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn example_1_lcs() {
let word1 = "sea";
let word2 = "eat";
let expected = 2;
let mut dp = vec![vec![None; word2.len() + 1]; word1.len() + 1];
assert_eq!(
Solution::lcs(word1, word2, word1.len(), word2.len(), &mut dp),
expected
);
}

#[test]
fn example_1() {
let word1 = "sea";
let word2 = "eat";
let expected = 2;
assert_eq!(
Solution::min_distance(word1.to_string(), word2.to_string()),
expected
);
}
}