diff --git a/src/list.rs b/src/list.rs new file mode 100644 index 0000000..26ce729 --- /dev/null +++ b/src/list.rs @@ -0,0 +1,33 @@ +// Definition for singly-linked list. +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option> +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { + next: None, + val + } + } +} + +pub fn make_list(arr: Vec) -> Option> { + let mut head: Option> = None; + let mut current = &mut head; + + for &value in arr.iter() { + let new_node = Box::new(ListNode::new(value)); + if current.is_none() { + *current = Some(new_node); + } else { + current.as_mut().unwrap().next = Some(new_node); + current = &mut current.as_mut().unwrap().next; + } + } + + head +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 529a0f6..bd2ada0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,106 +1,57 @@ -use std::cmp::min; -use std::collections::{BTreeSet, HashMap}; +use std::collections::HashSet; +use crate::list::{make_list, ListNode}; mod arr; - -#[derive(Debug)] -struct Pair { - index: i32, - value: i32, -} -impl PartialEq for Pair { - fn eq(&self, other: &Pair) -> bool { - self.value == other.value && self.index == other.index - } -} -impl Eq for Pair {} -impl PartialOrd for Pair { - fn partial_cmp(&self, other: &Pair) -> Option { - Some(self.cmp(other)) - } -} -impl Ord for Pair { - fn cmp(&self, other: &Pair) -> std::cmp::Ordering { - if self.value == other.value { - other.index.cmp(&self.index) - } else { - other.value.cmp(&self.value) - } - } -} - -impl Clone for Pair { - fn clone(&self) -> Self { - Pair { - index: self.index, - value: self.value, - } - } -} +mod list; struct Solution; + impl Solution { - pub fn find_x_sum(nums: Vec, k: i32, x: i32) -> Vec { - let x = x as usize; - let mut tree: BTreeSet = BTreeSet::new(); - let mut dict: HashMap = HashMap::new(); - let mut ans: Vec = vec![]; - for i in 0..k as usize { - let mut pair = Pair { - index: nums[i], - value: 1, - }; - if dict.contains_key(&nums[i]) { - let exist_pair = dict.get(&nums[i]).unwrap(); - tree.remove(exist_pair); - pair.value += exist_pair.value; + pub fn modified_list(nums: Vec, head: Option>) -> Option> { + let mut set:HashSet = nums.into_iter().collect(); + let mut cur = &head; + while let Some(node) = cur { + if set.contains(&node.val) { + cur = &node.next; + }else { + break; } - dict.insert(nums[i], pair.clone()); - tree.insert(pair); } - ans.push( - tree.iter() - .take(min(tree.len(), x)) - .fold(0, |last, cur| last + cur.value * cur.index), - ); - for i in 0..nums.len() - k as usize { - let mut left_item = dict[&nums[i]].clone(); - tree.remove(&left_item); - if left_item.value > 1 { - left_item.value -= 1; - dict.insert(nums[i], left_item.clone()); - tree.insert(left_item); + let mut dummy_head = Box::new(ListNode{val:0, next: cur.clone()}); + let mut last = &mut dummy_head.next; + + // let mut cur = &mut(last.as_mut()?.next); + while let Some(node) = last { + if node.next.is_none() {break;} + if set.contains(&node.next.as_ref().as_ref()?.val) { + // this could be a shit! + let mut next_node = last.as_mut().unwrap().next.as_mut().unwrap().next.take(); + let mut st = false; + while let Some(nd) = &next_node { + if set.contains(&nd.as_ref().val) { + next_node = next_node.as_mut().unwrap().next.take(); + }else { + st = true; + break; + } + } + if !st { + last.as_mut().unwrap().next = None; + }else { + last.as_mut().unwrap().next = next_node; + } + last = &mut last.as_mut().unwrap().next; } else { - dict.remove(&nums[i]); + last = &mut last.as_mut().unwrap().next; } - if dict.contains_key(&nums[i + k as usize]) { - let mut pair = dict.get(&nums[i + k as usize]).unwrap().clone(); - tree.remove(&pair); - pair.value += 1; - dict.insert(nums[i + k as usize], pair.clone()); - tree.insert(pair); - } else { - let pair = Pair { - index: nums[i + k as usize], - value: 1, - }; - dict.insert(nums[i + k as usize], pair.clone()); - tree.insert(pair); - }; - ans.push( - tree.iter() - .take(min(tree.len(), x)) - .fold(0, |last, cur| last + cur.value * cur.index), - ); } - ans + dummy_head.next } } fn main() { - let nums = vec![1, 1, 2, 2, 3, 4, 2, 3]; - let k = 6; - let x = 2; - let result = Solution::find_x_sum(nums, k, x); + let nums = vec![1]; + let head = make_list(vec![1,2,1,2,1,2]); + let result = Solution::modified_list(nums, head); println!("{:?}", result); }