Compare commits
72
Commits
08f6ce4581
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
551db6decf | ||
|
|
c1fe8fd658 | ||
|
|
23a4622f11 | ||
|
|
ebd3f9445c | ||
|
|
50fba25511 | ||
|
|
ece3af5149 | ||
|
|
a19c7573a7 | ||
|
|
fe0577dd06 | ||
|
|
fcf3f21a52 | ||
|
|
cb73a780cf | ||
|
|
86ec2ee252 | ||
|
|
fc8857537e | ||
|
|
8cc7ce864c | ||
|
|
b88bfb137b
|
||
|
|
985c978958
|
||
|
|
25645920ff
|
||
|
|
44ce8b9fd4
|
||
|
|
afa91fe380
|
||
|
|
6e59cabd84
|
||
|
|
00bc762b5e
|
||
|
|
9452c4e658 | ||
|
|
a9da2f1227
|
||
|
|
fa937a142f
|
||
|
|
6213d83856
|
||
|
|
d0da066c58
|
||
|
|
4460a2f028
|
||
|
|
4ee2acd245
|
||
|
|
6074be16ce
|
||
|
|
11a7d6775d
|
||
|
|
93d4b712d5
|
||
|
|
a467de459b
|
||
|
|
653e68c8f1
|
||
|
|
671347dfde
|
||
|
|
023fe9ec14
|
||
|
|
a6ca63686a
|
||
|
|
283b4c1518
|
||
|
|
444863b22d
|
||
|
|
8cac824082
|
||
|
|
8cc2ab780c
|
||
|
|
98eafcac0e
|
||
|
|
ecb53e3f3a
|
||
|
|
cca3dc4a88
|
||
|
|
8a381984c9 | ||
|
|
d7f5b98838 | ||
|
|
f1f47604f8 | ||
|
|
60447f6bf8 | ||
|
|
b0641080b5 | ||
|
|
bb6a7dc773 | ||
|
|
bb640829ee | ||
|
|
6c8fa06ce1 | ||
|
|
143ff15f87 | ||
|
|
d5eafc86f4 | ||
|
|
f91a4b2fe6 | ||
|
|
3d72b848d2 | ||
|
|
bde9d0f8b2 | ||
|
|
e615d4f611 | ||
|
|
f80a39e514 | ||
|
|
bd8207767a | ||
|
|
c18ad8c1b0 | ||
|
|
566c6a8414 | ||
|
|
f8fd02dcb8 | ||
|
|
563c0b3589 | ||
|
|
1772ba6c99 | ||
|
|
495075eef2 | ||
|
|
0f894b29a8 | ||
|
|
54bea679f0 | ||
|
|
873bf741e6 | ||
|
|
7efe4d98bb | ||
|
|
0f8400d0c9 | ||
|
|
9a82ec82f7 | ||
|
|
376065acb6 | ||
|
|
cd8b2b31e3 |
+123
@@ -79,3 +79,126 @@ pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||
|
||||
rows
|
||||
}
|
||||
|
||||
pub fn make_char_matrix(s: &str) -> Vec<Vec<char>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<char>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<char> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
' '
|
||||
} else {
|
||||
x.to_string().chars().nth(0).unwrap()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
pub fn make_string_matrix(s: &str) -> Vec<Vec<String>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<String>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<String> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
"".to_string()
|
||||
} else {
|
||||
x.to_string()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
+20
-35
@@ -1,44 +1,29 @@
|
||||
use crate::arr::make_matrix;
|
||||
use std::collections::HashSet;
|
||||
|
||||
struct Solution;
|
||||
mod arr;
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn max_k_divisible_components(
|
||||
n: i32,
|
||||
edges: Vec<Vec<i32>>,
|
||||
values: Vec<i32>,
|
||||
k: i32,
|
||||
) -> i32 {
|
||||
let mut edge = vec![HashSet::new(); n as usize];
|
||||
edges.iter().for_each(|x| {
|
||||
edge[x[0] as usize].insert(x[1] as usize);
|
||||
edge[x[1] as usize].insert(x[0] as usize);
|
||||
});
|
||||
fn dfs(total_ans:&mut i32, k:i32, root: usize, last: usize, edge: &Vec<HashSet<usize>>,values: &Vec<i32>) -> i64{
|
||||
let mut ans = values[root] as i64;
|
||||
for &t in edge[root].iter() {
|
||||
if t == last { continue; }
|
||||
ans += dfs(total_ans,k,t, root, edge,values);
|
||||
}
|
||||
if ans % k as i64 == 0 {
|
||||
*total_ans += 1;
|
||||
0
|
||||
}else {
|
||||
ans
|
||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||
let word1 = word1.chars().collect::<Vec<char>>();
|
||||
let word2 = word2.chars().collect::<Vec<char>>();
|
||||
for i in 1..=word1.len() {
|
||||
dp[i][0] = dp[i - 1][0] + 1;
|
||||
}
|
||||
for i in 1..=word2.len() {
|
||||
dp[0][i] = dp[0][i - 1] + 1;
|
||||
}
|
||||
for i in 0..word1.len() {
|
||||
for j in 0..word2.len() {
|
||||
dp[i + 1][j + 1] = (dp[i][j] + if word1[i] != word2[j] { 1 } else { 0 })
|
||||
.min(dp[i][j + 1] + 1)
|
||||
.min(dp[i + 1][j] + 1);
|
||||
}
|
||||
}
|
||||
let mut ans = 0;
|
||||
dfs(&mut ans,k,0, usize::MAX, &edge,&values);
|
||||
ans
|
||||
dp[word1.len()][word2.len()]
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
let result = Solution::max_k_divisible_components(
|
||||
7,
|
||||
make_matrix("[[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]"),
|
||||
vec![3, 0, 6, 1, 5, 2, 1],
|
||||
3,
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::min_distance("intention".to_string(), "execution".to_string())
|
||||
);
|
||||
println!("{:?}", result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user