Skip to content

Commit 4823159

Browse files
committed
match 190
1 parent c809035 commit 4823159

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ mod prob_5388;
425425
mod prob_5389;
426426
mod prob_5390;
427427
mod prob_5391;
428+
mod match_190;
428429
mod prob_5437;
429430
mod sword_offer;
430431
mod crack_code_interview;

src/match_190/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod prob_5416;
2+
mod prob_5417;
3+
mod prob_5418;
4+
mod prob_5419;

src/match_190/prob_5416.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn is_prefix_of_word(sentence: String, search_word: String) -> i32 {
3+
let search = search_word.as_str();
4+
for (idx, s) in sentence.split(" ").enumerate() {
5+
if s.starts_with(search) {
6+
return idx as i32+1;
7+
}
8+
}
9+
return -1;
10+
}
11+
}
12+
13+
struct Solution;

src/match_190/prob_5417.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
impl Solution {
3+
pub fn max_vowels(s: String, k: i32) -> i32 {
4+
let s = s.as_bytes();
5+
let mut count = 0;
6+
for i in 0..k as usize {
7+
if Self::is_vowel(s[i]) {
8+
count += 1;
9+
}
10+
}
11+
let mut ans = count;
12+
let mut i = 0;
13+
let mut j = k as usize;
14+
while j < s.len() {
15+
if Self::is_vowel(s[i]) {
16+
count-=1;
17+
}
18+
if Self::is_vowel(s[j]) {
19+
count+=1;
20+
ans = ans.max(count);
21+
}
22+
i+=1;
23+
j+=1;
24+
}
25+
ans
26+
}
27+
fn is_vowel(c: u8) -> bool {
28+
match c {
29+
b'a'|b'e'|b'i'|b'o'|b'u' => true,
30+
_ => false,
31+
}
32+
}
33+
}
34+
35+
struct Solution;

src/match_190/prob_5418.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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) -> Self {
12+
TreeNode {
13+
val,
14+
left: None,
15+
right: None
16+
}
17+
}
18+
}
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn pseudo_palindromic_paths (root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
23+
if root.is_none() {
24+
return 0;
25+
}
26+
let mut count = [0; 10];
27+
let mut ans = 0;
28+
Self::dfs(&root, &mut count, &mut ans);
29+
ans
30+
}
31+
fn dfs(r: &Option<Rc<RefCell<TreeNode>>>, count: &mut [i32; 10], ans: &mut i32) {
32+
if r.is_none() {
33+
return;
34+
}
35+
let br = r.as_ref().unwrap().borrow();
36+
if br.left.is_none() && br.right.is_none() {
37+
count[br.val as usize] += 1;
38+
let mut odd = 0;
39+
for i in 0..=9 {
40+
if count[i] & 1 == 1 {
41+
odd += 1;
42+
}
43+
}
44+
if odd <= 1 {
45+
*ans += 1;
46+
}
47+
count[br.val as usize] -= 1;
48+
return
49+
}
50+
count[br.val as usize] += 1;
51+
Self::dfs(&br.left, count, ans);
52+
Self::dfs(&br.right, count, ans);
53+
count[br.val as usize] -= 1;
54+
}
55+
}
56+
57+
struct Solution;
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_pseudo_palindromic_paths() {
65+
let root = Some(Rc::new(RefCell::new(TreeNode{
66+
val: 1,
67+
left: Some(Rc::new(RefCell::new(TreeNode{
68+
val: 9,
69+
left: None,
70+
right: Some(Rc::new(RefCell::new(TreeNode{
71+
val: 1,
72+
left: None,
73+
right: None,
74+
}))),
75+
}))),
76+
right: Some(Rc::new(RefCell::new(TreeNode{
77+
val: 1,
78+
left: None,
79+
right: Some(Rc::new(RefCell::new(TreeNode{
80+
val: 1,
81+
left: Some(Rc::new(RefCell::new(TreeNode{
82+
val: 7,
83+
left: None,
84+
right: Some(Rc::new(RefCell::new(TreeNode{
85+
val: 4,
86+
left: None,
87+
right: None,
88+
}))),
89+
}))),
90+
right: None,
91+
}))),
92+
}))),
93+
})));
94+
assert_eq!(Solution::pseudo_palindromic_paths(root), 1);
95+
}
96+
}

src/match_190/prob_5419.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
impl Solution {
2+
pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
3+
let n = nums1.len();
4+
let m = nums2.len();
5+
if n == 1 && m == 1 {
6+
return nums1[0]*nums2[0];
7+
}
8+
let mut f = vec![vec![std::i32::MIN; m+1]; n+1];
9+
for i in 0..=m {
10+
f[0][i] = 0;
11+
}
12+
for i in 0..=n {
13+
f[i][0] = 0;
14+
}
15+
let mut ans = std::i32::MIN;
16+
for i in 1..=n {
17+
for j in 1..=m {
18+
f[i][j] = f[i-1][j-1] + nums1[i-1]*nums2[j-1];
19+
ans = ans.max(f[i][j]);
20+
f[i][j] = f[i][j].max(f[i-1][j]);
21+
f[i][j] = f[i][j].max(f[i][j-1]);
22+
}
23+
}
24+
ans
25+
}
26+
}
27+
28+
struct Solution;
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::Solution;
33+
34+
#[test]
35+
fn test_max_dot_product() {
36+
let test_cases = vec![
37+
(vec![-3,-8,3,-10,1,3,9],vec![9,2,3,7,-9,1,-8,5,-1,-1],200),
38+
(vec![2,1,-2,5], vec![3,0,-6], 18),
39+
(vec![3, -2], vec![2,-6,7], 21),
40+
(vec![-1,-1],vec![1,1], -1),
41+
];
42+
for (nums1, nums2, expect) in test_cases {
43+
assert_eq!(Solution::max_dot_product(nums1.clone(), nums2.clone()), expect, "nums1: {:?}, nums2: {:?}", nums1, nums2);
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)