From 941d05d9ee418b18ab6360f5c2f14c031de726ad Mon Sep 17 00:00:00 2001 From: li-chx Date: Tue, 4 Nov 2025 16:23:28 +0800 Subject: [PATCH] =?UTF-8?q?3318.=20=E8=AE=A1=E7=AE=97=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=20x-sum=20I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 153 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 59 deletions(-) diff --git a/src/main.rs b/src/main.rs index 25971f0..529a0f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,71 +1,106 @@ +use std::cmp::min; +use std::collections::{BTreeSet, HashMap}; + mod arr; -struct Bank { - _balance: Vec, + +#[derive(Debug)] +struct Pair { + index: i32, + value: i32, } - - -/** - * `&self` means the method takes an immutable reference. - * If you need a mutable reference, change it to `&mut self` instead. - */ -impl Bank { - - fn new(balance: Vec) -> Self { - Bank { _balance: balance } +impl PartialEq for Pair { + fn eq(&self, other: &Pair) -> bool { + self.value == other.value && self.index == other.index } - - fn valid(&self, account: i32,money: i64) -> bool { - account > 0 && account as usize <= self._balance.len() && self._balance[account as usize - 1] >= money +} +impl Eq for Pair {} +impl PartialOrd for Pair { + fn partial_cmp(&self, other: &Pair) -> Option { + Some(self.cmp(other)) } - fn transfer(&mut self, account1: i32, account2: i32, money: i64) -> bool { - if !self.valid(account1, money) { - return false; - } - self._balance[account1 as usize - 1] -= money; - self._balance[account2 as usize - 1] += money; - true - } - - fn deposit(&mut self, account: i32, money: i64) -> bool { - if !self.valid(account, 0) { - return false; - } - self._balance[account as usize - 1] += money; - true - } - - fn withdraw(&mut self, account: i32, money: i64) -> bool { - if !self.valid(account, money) { - false +} +impl Ord for Pair { + fn cmp(&self, other: &Pair) -> std::cmp::Ordering { + if self.value == other.value { + other.index.cmp(&self.index) } else { - self._balance[account as usize - 1] -= money; - true + other.value.cmp(&self.value) } } } -/** - * Your Bank object will be instantiated and called as such: - * let obj = Bank::new(balance); - * let ret_1: bool = obj.transfer(account1, account2, money); - * let ret_2: bool = obj.deposit(account, money); - * let ret_3: bool = obj.withdraw(account, money); - */ +impl Clone for Pair { + fn clone(&self) -> Self { + Pair { + index: self.index, + value: self.value, + } + } +} + +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; + } + 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); + } else { + dict.remove(&nums[i]); + } + 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 + } +} fn main() { - let oper = ["withdraw", "transfer", "deposit", "transfer", "withdraw"]; - let val = arr::make_matrix("[[3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]"); - //输出: - //[true, true, true, false, false] - let mut bank = Bank::new(vec![10, 100, 20, 50, 30]); - for i in 0..oper.len() { - let result = match oper[i] { - "withdraw" => bank.withdraw(val[i][0], val[i][1] as i64), - "transfer" => bank.transfer(val[i][0], val[i][1], val[i][2] as i64), - "deposit" => bank.deposit(val[i][0], val[i][1] as i64), - _ => false, - }; - println!("{}", result); - } - + 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); + println!("{:?}", result); }