Skip to content

Commit 38e9fc3

Browse files
committed
daily
1 parent 095a1b4 commit 38e9fc3

File tree

5 files changed

+158
-42
lines changed

5 files changed

+158
-42
lines changed

.idea/workspace.xml

Lines changed: 24 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dailies/d20_12_12.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Definition for a binary tree node.
2+
#[derive(Debug, PartialEq, Eq)]
3+
pub struct TreeNode {
4+
pub val: i32,
5+
pub left: Option<Rc<RefCell<TreeNode>>>,
6+
pub right: Option<Rc<RefCell<TreeNode>>>,
7+
}
8+
9+
impl TreeNode {
10+
#[inline]
11+
pub fn new(val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
12+
Some(Rc::new(RefCell::new( TreeNode {
13+
val,
14+
left,
15+
right
16+
})))
17+
}
18+
}
19+
#[cfg(test)]
20+
mod tests {
21+
use crate::dailies::d20_12_12::{Solution, TreeNode};
22+
use std::rc::Rc;
23+
use std::cell::RefCell;
24+
25+
#[test]
26+
fn it_works() {
27+
assert_eq!(2, Solution::subtree_with_all_deepest(
28+
TreeNode::new(3,
29+
TreeNode::new(5,
30+
TreeNode::new(6, None, None),
31+
TreeNode::new(2,
32+
TreeNode::new(7, None, None),
33+
TreeNode::new(4, None, None))),
34+
TreeNode::new(1,
35+
TreeNode::new(0, None, None),
36+
TreeNode::new(8, None, None)))
37+
).unwrap().borrow().val);
38+
}
39+
}
40+
41+
42+
struct Solution;
43+
44+
use std::rc::Rc;
45+
use std::cell::RefCell;
46+
impl Solution {
47+
pub fn subtree_with_all_deepest(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
48+
if root.is_none() {
49+
return root;
50+
}
51+
let mut deepest_leaves = Vec::new();
52+
let mut max_depth = 0;
53+
Solution::traverse(1, None, &root.unwrap(), &mut deepest_leaves, &mut max_depth);
54+
55+
//moveup on all leaves until they hit same value
56+
let mut val = deepest_leaves[0].as_ref().unwrap().node.borrow().val;
57+
while !deepest_leaves[1..].iter().all(|l|l.as_ref().unwrap().node.borrow().val == val) {
58+
for i in 0..deepest_leaves.len() {
59+
deepest_leaves[i] = deepest_leaves[i].as_ref().unwrap().parent.clone();
60+
}
61+
val = deepest_leaves[0].as_ref().unwrap().node.borrow().val;
62+
}
63+
64+
Some(deepest_leaves[0].as_ref().unwrap().node.clone())
65+
}
66+
67+
fn traverse(depth:u32, parent:Option<Rc<UpNode>>, node:&NodePointer, deepest_leaves:&mut Vec<Option<Rc<UpNode>>>, max_depth:&mut u32){
68+
let this_node = Some(Rc::new(UpNode{node:node.clone(), parent }));
69+
let node = node.borrow();
70+
if node.left.is_none() && node.right.is_none(){
71+
if depth >= *max_depth {
72+
if depth > *max_depth {
73+
*max_depth = depth;
74+
deepest_leaves.clear();
75+
}
76+
deepest_leaves.push(this_node);
77+
}
78+
return;
79+
}
80+
81+
if let Some(l) = &node.left {
82+
Solution::traverse(depth + 1, this_node.clone(), l, deepest_leaves, max_depth);
83+
}
84+
85+
if let Some(r) = &node.right {
86+
Solution::traverse(depth + 1, this_node.clone(), r, deepest_leaves, max_depth);
87+
}
88+
}
89+
}
90+
type NodePointer = Rc<RefCell<TreeNode>>;
91+
struct UpNode {
92+
node:NodePointer,
93+
parent:Option<Rc<UpNode>>
94+
}

src/dailies/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//mod d20_12_9;
22
//mod d20_12_10;
3-
//mod d20_12_11;
3+
//mod d20_12_11;
4+
mod d20_12_12;

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ mod dailies;
1010
//mod p0006;
1111
//mod p0007;
1212
//mod p0008;
13-
mod p0009;
13+
//mod p0009;
1414
//mod p0010;
1515

1616

1717
fn main() {
18-
let test = p0009::Solution::is_palindrome(121);
18+
//let test = p0009::Solution::is_palindrome(121);
1919
println!("{}","");
2020
}

src/p0010.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11

2-
32
pub(crate) struct Solution;
43

54
enum Token {
6-
Char(char),
5+
Char(u8),
76
Star(Box<Token>),
87
Dot
98
}
109

10+
#[derive(Clone)]
11+
enum Match {
12+
None,
13+
One(usize),
14+
Star(StarMatch)
15+
}
16+
17+
#[derive(Clone)]
18+
struct StarMatch {
19+
index:usize,
20+
length:usize,
21+
}
22+
1123
impl Solution {
1224
pub fn is_match(s: String, p: String) -> bool {
25+
let chars = s.as_bytes();
1326
let pattern = Solution::parse_pattern(p);
14-
let chars: Vec<char> = s.chars().collect();
15-
let mut i = 0;
27+
let mut matches = vec![Match::None; pattern.len()];
1628

17-
for t in pattern {
18-
if !Solution::matcher(&t, &mut i, &chars) {
19-
return false;
29+
let mut token_i = 0;
30+
let mut char_i = 0;
31+
loop {
32+
matches[token_i] = match &pattern[token_i] {
33+
Token::Char(c) if c == chars[char_i] => unimplemented!(),
34+
Token::Star(t) => unimplemented!(),
35+
Token::Dot => unimplemented!(),
2036
}
2137
}
22-
if i == chars.len() {
23-
true
24-
} else {
25-
false
26-
}
38+
39+
unimplemented!()
2740
}
2841

2942
fn char(c: &char, i: &mut usize, chars: &Vec<char>) -> bool {
@@ -40,17 +53,17 @@ impl Solution {
4053
true
4154
}
4255

43-
fn matcher(t: &Token, i: &mut usize, chars: &Vec<char>) -> bool {
44-
match t {
45-
Token::Char(c) => if *i >= chars.len() || !Solution::char(c, i, chars) { return false; }
46-
Token::Dot => if *i >= chars.len() || !Solution::dot(i) { return false; }
47-
Token::Star(t) => {
48-
//TODO make less greedy
49-
while Solution::matcher(t, i, chars) {}
50-
}
51-
}
52-
return true;
53-
}
56+
// fn matcher(t: &Token, i: &mut usize, chars: &Vec<char>) -> Match {
57+
// match t {
58+
// Token::Char(c) => if *i >= chars.len() || !Solution::char(c, i, chars) { return false; }
59+
// Token::Dot => if *i >= chars.len() || !Solution::dot(i) { return false; }
60+
// Token::Star(t) => {
61+
// //TODO make less greedy
62+
// while Solution::matcher(t, i, chars) {}
63+
// }
64+
// }
65+
// return true
66+
// }
5467

5568

5669
fn parse_pattern<'a>(p: String) -> Vec<Token> {

0 commit comments

Comments
 (0)