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 - */