Compare commits

..
29 Commits
Author SHA1 Message Date
li_chx 551db6decf 72. 编辑距离 2026-08-13 11:16:29 +08:00
li_chx c1fe8fd658 215. 数组中的第K个最大元素 2026-08-13 10:29:17 +08:00
li_chx 23a4622f11 1510. 石子游戏 IV 2026-08-13 10:29:03 +08:00
li_chx ebd3f9445c 1406. 石子游戏 III 2026-08-13 10:28:46 +08:00
li_chx 50fba25511 877. 石子游戏 2026-08-13 10:28:19 +08:00
li_chx ece3af5149 2996. 大于等于顺序前缀和的最小缺失整数 2026-08-13 10:27:10 +08:00
li_chx a19c7573a7 2958. 最多 K 个重复元素的最长子数组 2026-08-13 10:27:08 +08:00
li_chx fe0577dd06 3739. 统计主要元素子数组数目 II 2026-08-12 11:08:31 +08:00
li_chx fcf3f21a52 93. 复原 IP 地址 2026-08-03 21:44:25 +08:00
li_chx cb73a780cf 3501. 操作后最大活跃区段数 II 2026-07-29 13:03:47 +08:00
li_chx 86ec2ee252 3499. 操作后最大活跃区段数 I 2026-07-28 18:35:53 +08:00
li_chx fc8857537e 3517. 最小回文排列 I 2026-07-28 17:25:58 +08:00
li_chx 8cc7ce864c 1331. 数组序号转换 2026-07-12 19:25:21 +08:00
li_chx b88bfb137b 3120. 统计特殊字母的数量 I 2026-05-26 08:40:54 +08:00
li_chx 985c978958 1871. 跳跃游戏 VII 2026-05-25 09:55:22 +08:00
li_chx 25645920ff 3043. 最长公共前缀的长度 2026-05-21 09:42:33 +08:00
li_chx 44ce8b9fd4 2540. 最小公共值 2026-05-19 11:44:18 +08:00
li_chx afa91fe380 1306. 跳跃游戏 III 2026-05-18 17:00:02 +08:00
li_chx 6e59cabd84 1345. 跳跃游戏 IV 2026-05-18 16:46:04 +08:00
li_chx 00bc762b5e 2553. 分割数组中数字的数位 2026-05-18 16:22:37 +08:00
li_chx 9452c4e658 3629. 通过质数传送到达终点的最少跳跃次数 2026-05-09 16:52:51 +08:00
li_chx a9da2f1227 788. 旋转数字 2026-05-02 17:45:45 +08:00
li_chx fa937a142f 3742. 网格中得分最大的路径 2026-04-30 08:58:35 +08:00
li_chx 6213d83856 2033. 获取单值网格的最小操作数 2026-04-28 09:33:19 +08:00
li_chx d0da066c58 1391. 检查网格中是否存在有效路径 2026-04-27 09:39:39 +08:00
li_chx 4460a2f028 2833. 距离原点最远的点 2026-04-24 08:50:07 +08:00
li_chx 4ee2acd245 2615. 等值距离和 2026-04-23 08:53:17 +08:00
li_chx 6074be16ce 2452. 距离字典两次编辑以内的单词 2026-04-22 08:46:21 +08:00
li_chx 11a7d6775d 1722. 执行交换操作后的最小汉明距离 2026-04-21 15:00:19 +08:00
+18 -40
View File
@@ -1,51 +1,29 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;
mod arr;
struct Solution {}
impl Solution {
fn get_rev(t: i32) -> i32 {
let mut t = t;
let mut ans = 0;
while t > 0 {
ans = ans * 10 + (t % 10);
t /= 10;
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;
}
ans
for i in 1..=word2.len() {
dp[0][i] = dp[0][i - 1] + 1;
}
pub fn min_mirror_pair_distance(nums: Vec<i32>) -> i32 {
let mut map = HashMap::<i32, Vec<usize>>::new();
for i in 0..nums.len() {
map.entry(nums[i]).or_insert_with(Vec::new).push(i);
}
let mut ans = 1e9 as usize;
for i in 0..nums.len() {
let x = nums[i];
match map.entry(Self::get_rev(x)) {
Entry::Occupied(arr) => {
//ans = ans.max(i.abs_diff(*arr.get().last().unwrap())).max(i.abs_diff(*arr.get().first().unwrap()));
let arr = arr.get();
if arr.len() == 1 {
if i != arr[0] && arr[0] > i {
ans = ans.min(arr[0] - i);
}
continue;
}
let idx = arr.partition_point(|y| i >= *y);
if idx < arr.len() {
if i != arr[idx] {
ans = ans.min(arr[idx] - i);
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);
}
}
}
Entry::Vacant(_) => {}
dp[word1.len()][word2.len()]
}
}
if ans == 1e9 as usize { -1 } else { ans as i32 }
}
}
fn main() {
println!("{:?}", Solution::min_mirror_pair_distance(vec![9,9]));
println!(
"{:?}",
Solution::min_distance("intention".to_string(), "execution".to_string())
);
}