From 653e68c8f129d351c0ef182708d53fe241ba8813 Mon Sep 17 00:00:00 2001 From: li_chx Date: Mon, 20 Apr 2026 09:18:53 +0800 Subject: [PATCH] =?UTF-8?q?1855.=20=E4=B8=8B=E6=A0=87=E5=AF=B9=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 52 +++++++++++----------------------------------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2727f9d..92f5252 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,52 +1,22 @@ -use crate::arr::make_string_arr; -use std::collections::HashMap; - mod arr; struct Solution {} impl Solution { - pub fn closest_target(words: Vec, target: String, start_index: i32) -> i32 { - let n = words.len(); - let map = words.into_iter().enumerate().fold( - HashMap::>::new(), - |mut acc, (i, word)| { - acc.entry(word).or_default().push(i); - acc - }, - ); - if map.contains_key(&target) { - let arr = map.get(&target).unwrap(); - let idx = arr.partition_point(|&x| x < start_index as usize); - let a = arr[idx % arr.len()].abs_diff(start_index as usize); - let b = arr[(idx + arr.len() - 1) % arr.len()].abs_diff(start_index as usize); - a.min(n - a).min(b).min(n - b) as i32 - } else { - -1 + pub fn max_distance(nums1: Vec, nums2: Vec) -> i32 { + let mut ans = 0; + for i in 0..nums1.len() { + let mut idx = nums2.partition_point(|&x| x >= nums1[i]); + if idx == 0 { + continue; + } + idx -= 1; + ans = ans.max(idx as i32 - i as i32); } + ans } } fn main() { - println!( - "{:?}", - Solution::closest_target( - make_string_arr(r#"["hello","i","am","leetcode","hello"]"#), - "hello".to_string(), - 1 - ) - ); + println!("{:?}", Solution::max_distance(vec![55,30,5,4,2],vec![100,20,10,10,5])); } -/* - -[[-86, 2], [-76, 2], [-46, 2]] -[-84, -59] -[[0, 2, 29], [0, 2, 10], [0, 2, 10]] -10 - - --8 -6 -4 -2 --86 -74 -46 -29 - -84 -60 -59 - -7 -5 -3 - */