Skip to content

Commit d896f57

Browse files
committed
12/15/2020
1 parent 3f3ccbd commit d896f57

8 files changed

+161
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ leetcode practise solution in rust
164164
|1678|[Goal Parser Interpretation](src/_1678_goal_parser_interpretation.rs)|
165165
|1679|[Max Number of K-Sum Pairs](src/_1679_max_number_of_k-sum_pairs.rs)|
166166
|1680|[Concatenation of Consecutive Binary Numbers](src/_1680_concatenation_of_consecutive_binary_numbers.rs)|
167+
|1684|[Count the Number of Consistent Strings](src/_1684_count_the_number_of_consistent_strings.rs)|
168+
|1685|[Sum of Absolute Differences in a Sorted Array](src/_1685_sum_of_absolute_differences_in_a_sorted_array.rs)|
169+
|1686|[Stone Game VI](src/_1686_stone_game_vi.rs)|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
5+
let mut allowed_chars: Vec<bool> = vec![false; 26];
6+
for v in allowed.chars() {
7+
allowed_chars[v as usize - 'a' as usize] = true;
8+
}
9+
10+
let mut result = 0;
11+
for word in words {
12+
let mut same = true;
13+
for v in word.chars() {
14+
if !allowed_chars[v as usize - 'a' as usize] {
15+
same = false;
16+
break;
17+
};
18+
}
19+
if same {
20+
result += 1;
21+
}
22+
}
23+
result
24+
}
25+
}
26+
27+
#[test]
28+
fn test() {
29+
assert_eq!(
30+
Solution::count_consistent_strings(
31+
"ab".to_string(),
32+
vec_string!["ad", "bd", "aaab", "baa", "badab"]
33+
),
34+
2
35+
);
36+
assert_eq!(
37+
Solution::count_consistent_strings(
38+
"abc".to_string(),
39+
vec_string!["a", "b", "c", "ab", "ac", "bc", "abc"]
40+
),
41+
7
42+
);
43+
assert_eq!(
44+
Solution::count_consistent_strings(
45+
"cad".to_string(),
46+
vec_string!["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]
47+
),
48+
4
49+
);
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct Solution;
2+
impl Solution {
3+
pub fn get_sum_absolute_differences(nums: Vec<i32>) -> Vec<i32> {
4+
let n = nums.len();
5+
let sum: i32 = nums.iter().sum();
6+
let mut prefix_sum = 0;
7+
let mut result = vec![];
8+
9+
for (i, v) in nums.iter().enumerate() {
10+
result.push((sum - prefix_sum) - v * (n as i32 - i as i32) + v * i as i32 - prefix_sum);
11+
prefix_sum += v;
12+
}
13+
result
14+
}
15+
}
16+
17+
#[test]
18+
fn test() {
19+
assert_eq!(
20+
Solution::get_sum_absolute_differences(vec![2, 3, 5]),
21+
vec![4, 3, 5]
22+
);
23+
assert_eq!(
24+
Solution::get_sum_absolute_differences(vec![1, 4, 6, 8, 10]),
25+
vec![24, 15, 13, 15, 21]
26+
);
27+
}

src/_1686_stone_game_vi.rs

Whitespace-only changes.

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,11 @@ mod _1609_even_odd_tree;
335335
mod _1679_max_number_of_ksum_pairs;
336336
//
337337
mod _1680_concatenation_of_consecutive_binary_numbers;
338+
//
339+
mod _1684_count_the_number_of_consistent_strings;
340+
//
341+
mod _1685_sum_of_absolute_differences_in_a_sorted_array;
342+
//
343+
mod _1686_stone_game_vi;
344+
//
345+
mod test;

src/test.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// use std::collections::HashMap;
2+
3+
// struct Solution;
4+
// // impl Solution {
5+
// // pub fn number_of_matches(n: i32) -> i32 {
6+
// // let mut n = n;
7+
// // let mut result = 0;
8+
9+
// // while n > 1 {
10+
// // if n % 2 == 0 {
11+
// // result += n / 2;
12+
// // n = n / 2;
13+
// // } else {
14+
// // result += (n - 1) / 2;
15+
// // n = (n - 1) / 2 + 1;
16+
// // }
17+
// // }
18+
19+
// // result
20+
// // }
21+
// // }
22+
23+
// // impl Solution {
24+
// // pub fn min_partitions(n: String) -> i32 {
25+
// // let mut result = 0;
26+
// // for v in n.chars() {
27+
// // if v as usize - '0' as usize > result {
28+
// // result = v as usize - '0' as usize;
29+
// // }
30+
// // }
31+
// // result as i32
32+
// // }
33+
// // }
34+
35+
// impl Solution {
36+
// pub fn stone_game_vii(stones: Vec<i32>) -> i32 {
37+
// let n = stones.len();
38+
// let mut prefix: Vec<i32> = vec![0; n + 1];
39+
// let mut temp = 0;
40+
// for i in 0..n {
41+
// temp += stones[i];
42+
// prefix[i + 1] = temp;
43+
// }
44+
// let mut dp_m:HashMap<(usize,usize), i32> = HashMap::new();
45+
// Self::dp(0, n - 1, &mut dp_m,&mut prefix, &stones)
46+
// }
47+
48+
// fn dp(l: usize, r: usize, dp_m:&mut HashMap<(usize,usize),i32>, prefix: &mut Vec<i32>, stones: &Vec<i32>,) -> i32 {
49+
// if l > r {
50+
// return 0;
51+
// } else {
52+
// if let Some(&res) = dp_m.get(&(l,r)){
53+
// return res;
54+
// }else {
55+
// let sum = prefix[r + 1] - prefix[l];
56+
// let right_max = sum - stones[l] - Self::dp(l + 1, r, dp_m,prefix, stones);
57+
// let left_max = sum - stones[r] - Self::dp(l, r - 1, dp_m,prefix, stones);
58+
// dbg!(&right_max.max(left_max).clone());
59+
// return right_max.max(left_max).clone();
60+
// }
61+
// }
62+
// }
63+
64+
// #[test]
65+
// fn test() {
66+
// assert_eq!(Solution::stone_game_vii(vec![5, 3, 1, 4, 2]), 6);
67+
// assert_eq!(
68+
// Solution::stone_game_vii(vec![7, 90, 5, 1, 100, 10, 10, 2]),
69+
// 122
70+
// );
71+
// }

target/.rustc_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"rustc_fingerprint":3710726291328818393,"outputs":{"16929990295604814582":["___\n",""],"1164083562126845933":["rustc 1.46.0 (04488afe3 2020-08-24)\nbinary: rustc\ncommit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9\ncommit-date: 2020-08-24\nhost: x86_64-apple-darwin\nrelease: 1.46.0\nLLVM version: 10.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chengsu/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
1+
{"rustc_fingerprint":4202038607004574977,"outputs":{"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chengsu/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""],"1164083562126845933":["rustc 1.48.0 (7eac88abb 2020-11-16)\nbinary: rustc\ncommit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\ncommit-date: 2020-11-16\nhost: x86_64-apple-darwin\nrelease: 1.48.0\nLLVM version: 11.0\n",""]},"successes":{}}

target/rls/.rustc_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"rustc_fingerprint":3710726291328818393,"outputs":{"16929990295604814582":["___\n",""],"1164083562126845933":["rustc 1.46.0 (04488afe3 2020-08-24)\nbinary: rustc\ncommit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9\ncommit-date: 2020-08-24\nhost: x86_64-apple-darwin\nrelease: 1.46.0\nLLVM version: 10.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chengsu/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
1+
{"rustc_fingerprint":4202038607004574977,"outputs":{"1164083562126845933":["rustc 1.48.0 (7eac88abb 2020-11-16)\nbinary: rustc\ncommit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\ncommit-date: 2020-11-16\nhost: x86_64-apple-darwin\nrelease: 1.48.0\nLLVM version: 11.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/chengsu/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}

0 commit comments

Comments
 (0)