72. 编辑距离
This commit is contained in:
+16
-12
@@ -1,25 +1,29 @@
|
|||||||
mod arr;
|
mod arr;
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
|
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||||
let up = 10000;
|
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||||
let mut k = k;
|
let word1 = word1.chars().collect::<Vec<char>>();
|
||||||
let mut cap = vec![0; up * 2 + 1];
|
let word2 = word2.chars().collect::<Vec<char>>();
|
||||||
for x in nums {
|
for i in 1..=word1.len() {
|
||||||
cap[x as usize + up] += 1;
|
dp[i][0] = dp[i - 1][0] + 1;
|
||||||
}
|
}
|
||||||
for i in (0..2 * up + 1).rev() {
|
for i in 1..=word2.len() {
|
||||||
if k <= cap[i] {
|
dp[0][i] = dp[0][i - 1] + 1;
|
||||||
return (i - up) as i32;
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
k -= cap[i];
|
|
||||||
}
|
}
|
||||||
0
|
dp[word1.len()][word2.len()]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
Solution::find_kth_largest(vec![3, 2, 3, 1, 2, 4, 5, 5, 6], 4)
|
Solution::min_distance("intention".to_string(), "execution".to_string())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user