Skip to content
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

Develop #40

Merged
merged 37 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a91d5a3
feat: new module
jtr109 Jun 8, 2021
d2f9c34
feat: add tests
jtr109 Jun 8, 2021
00503c0
feat: tests passed
jtr109 Jun 8, 2021
64b9a3d
feat: resolved
jtr109 Jun 8, 2021
70dff1b
feat: add module
jtr109 Jun 9, 2021
fdced4f
feat: add tests
jtr109 Jun 9, 2021
fb856a1
feat: resolved without cache
jtr109 Jun 9, 2021
d99c0cb
feat: variable res
jtr109 Jun 9, 2021
6b5d073
feat: add cache
jtr109 Jun 9, 2021
b10b5ec
feat: resovled but timed out
jtr109 Jun 9, 2021
657e97c
feat: resolved by iterator but also timed out
jtr109 Jun 9, 2021
d32a082
feat: add refine strategy
jtr109 Jun 9, 2021
4379e0a
feat: add note
jtr109 Jun 10, 2021
3843806
feat: use queue to store max
jtr109 Jun 10, 2021
b25d205
feat: add tests
jtr109 Jun 10, 2021
ae1372e
feat: resolved
jtr109 Jun 10, 2021
266e86c
feat: new module
jtr109 Jun 11, 2021
27a1268
feat: add tests
jtr109 Jun 11, 2021
cc3d268
feat: resolved
jtr109 Jun 11, 2021
4920d8b
feat: use anchor
jtr109 Jun 11, 2021
6e9f12b
feat: add sum cache
jtr109 Jun 11, 2021
960c11a
feat: add tests
jtr109 Jun 12, 2021
9fffb11
feat: resolved but not understand
jtr109 Jun 12, 2021
f8e41b7
feat: add tests
jtr109 Jun 13, 2021
f300568
feat: add method to validate palindrome
jtr109 Jun 13, 2021
31168d5
feat: resolved but timed out
jtr109 Jun 13, 2021
47b3bd9
feat: add TODO
jtr109 Jun 14, 2021
45e3e53
feat: add tests
jtr109 Jun 15, 2021
ffbd09b
feat: resovled
jtr109 Jun 15, 2021
7fe0cc0
feat: add tests
jtr109 Jun 16, 2021
43353b2
feat: resolve examples but a submission failed
jtr109 Jun 16, 2021
2d6edf6
feat: resolved
jtr109 Jun 16, 2021
5e38703
feat: add a module
jtr109 Jun 17, 2021
df17686
feat: add a test
jtr109 Jun 17, 2021
8559963
feat: wrong answer when submitted
jtr109 Jun 17, 2021
8982c3a
feat: submission_2 unresolved
jtr109 Jun 17, 2021
c207880
feat: resolved
jtr109 Jun 17, 2021
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
36 changes: 36 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ members = [
"maximum_performance_of_a_team",
"longest_consecutive_sequence",
"min_cost_climbing_stairs",
"construct_binary_tree_from_preorder_and_inorder_traversal",
"jump_game_vi",
"my_calendar_i",
"stone_game_vii",
"minumum_number_of_refueling_stops",
"palindrome_pairs",
"matchsticks_to_square",
"generate_parentheses",
"number_of_subarrays_with_bounded_maximum",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "construct_binary_tree_from_preorder_and_inorder_traversal"
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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::cell::RefCell;
use std::ops::Index;
use std::rc::Rc;

// 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,
}
}
}

pub struct Solution {}

impl Solution {
pub fn build_tree(preorder: Vec<i32>, inorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
match preorder.iter().nth(0) {
None => None,
Some(val) => {
let index = inorder.iter().position(|v| val == v).unwrap();
let left_inorder = inorder[..index].to_vec();
let left_preorder = preorder[1..1 + index].to_vec();
let right_inorder = inorder[index + 1..].to_vec();
let right_preorder = preorder[1 + index..].to_vec();
Some(Rc::new(RefCell::new(TreeNode {
val: *val,
left: Self::build_tree(left_preorder, left_inorder),
right: Self::build_tree(right_preorder, right_inorder),
})))
}
}
}
}

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

#[test]
fn example_1() {
let preorder = [3, 9, 20, 15, 7];
let inorder = [9, 3, 15, 20, 7];
let expected = Some(Rc::new(RefCell::new(TreeNode {
val: 3,
left: Some(Rc::new(RefCell::new(TreeNode {
val: 9,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 20,
left: Some(Rc::new(RefCell::new(TreeNode {
val: 15,
left: None,
right: None,
}))),
right: Some(Rc::new(RefCell::new(TreeNode {
val: 7,
left: None,
right: None,
}))),
}))),
})));
assert_eq!(
Solution::build_tree(preorder.to_vec(), inorder.to_vec()),
expected
);
}

#[test]
fn example_2() {
let preorder = [-1];
let inorder = [-1];
let expected = Some(Rc::new(RefCell::new(TreeNode::new(-1))));
assert_eq!(
Solution::build_tree(preorder.to_vec(), inorder.to_vec()),
expected
);
}
}
9 changes: 9 additions & 0 deletions generate_parentheses/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "generate_parentheses"
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]
67 changes: 67 additions & 0 deletions generate_parentheses/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*!
* Follow solution: https://leetcode.com/problems/generate-parentheses/discuss/10100/Easy-to-understand-Java-backtracking-solution
*/

pub struct Solution {}

impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut list = Vec::new();
Self::backtrack(&mut list, "".to_string(), 0, 0, n as usize);
list
}

fn backtrack(list: &mut Vec<String>, s: String, open: usize, close: usize, max: usize) {
// 如果前缀为 s 的值,获取所有可能性,加入 list
if (s.chars().count()) == max * 2 {
list.push(s);
return;
}

if open < max {
// 如果前缀中的 `(` 数量达到最大值,不再往前缀中加入 `(`
Self::backtrack(list, s.clone() + "(", open + 1, close, max);
}
if close < open {
// 如果前缀中的 `)` 数量和 `(` 数量一样多了,不再往前缀中加入 `)`
Self::backtrack(list, s.clone() + ")", open, close + 1, max);
}
}
}

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

fn it_works(n: i32, expected: Vec<&str>) {
let mut result = Solution::generate_parenthesis(n);
let mut exp = expected.to_vec();
result.sort();
exp.sort();
assert_eq!(result, exp);
}

#[test]
fn example_1() {
let n = 3;
let expected = ["((()))", "(()())", "(())()", "()(())", "()()()"];
it_works(n, expected.to_vec());
}

#[test]
fn example_2() {
let n = 1;
let expected = ["()"];
it_works(n, expected.to_vec());
}

#[test]
fn submission_1() {
let n = 4;
let expected = [
"(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()",
"(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()",
];
it_works(n, expected.to_vec());
}
}
9 changes: 9 additions & 0 deletions jump_game_vi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "jump_game_vi"
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]
Loading