|
| 1 | +// 2461. Maximum Sum of Distinct Subarrays With Length K |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/ |
| 5 | +// |
| 6 | +// Tags: Array - Hash Table - Sliding Window |
| 7 | + |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +struct Solution; |
| 11 | +impl Solution { |
| 12 | + /// Use a sliding window of size k, keep its sum and the count of elements in the window, also |
| 13 | + /// the number of duplicates, add values from the right and pop them from the left updating the |
| 14 | + /// counts, sum and duplicates, if the current window does not have duplicates, max its value |
| 15 | + /// with the result. |
| 16 | + /// |
| 17 | + /// Time complexity: O(n) - We process each element in the input in O(1~) |
| 18 | + /// Space complexity: O(n) - The counts hashmap could have one entry per value in the input |
| 19 | + /// vector. |
| 20 | + /// |
| 21 | + /// Runtime 32 ms Beats 5% |
| 22 | + /// Memory 7.11 MB Beats 9% |
| 23 | + pub fn maximum_subarray_sum(nums: Vec<i32>, k: i32) -> i64 { |
| 24 | + let mut counts = HashMap::<i32, usize>::new(); |
| 25 | + let mut duplicates = 0; |
| 26 | + let k = k as usize; |
| 27 | + let n = nums.len(); |
| 28 | + let mut current_sum = 0i64; |
| 29 | + let mut res = 0i64; |
| 30 | + for &num in &nums[..k] { |
| 31 | + current_sum += num as i64; |
| 32 | + counts.entry(num).and_modify(|c| *c += 1).or_insert(1); |
| 33 | + if counts[&num] == 2 { |
| 34 | + duplicates += 1; |
| 35 | + } |
| 36 | + } |
| 37 | + if duplicates == 0 { |
| 38 | + res = current_sum; |
| 39 | + } |
| 40 | + let mut l = 0; |
| 41 | + for r in k..n { |
| 42 | + current_sum += nums[r] as i64; |
| 43 | + counts.entry(nums[r]).and_modify(|c| *c += 1).or_insert(1); |
| 44 | + if counts[&nums[r]] == 2 { |
| 45 | + duplicates += 1; |
| 46 | + } |
| 47 | + current_sum -= nums[l] as i64; |
| 48 | + counts.entry(nums[l]).and_modify(|c| *c -= 1); |
| 49 | + if counts[&nums[l]] == 1 { |
| 50 | + duplicates -= 1; |
| 51 | + } |
| 52 | + if duplicates == 0 { |
| 53 | + res = res.max(current_sum); |
| 54 | + } |
| 55 | + l += 1; |
| 56 | + } |
| 57 | + res |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Tests. |
| 62 | +fn main() { |
| 63 | + let tests = [(vec![1, 5, 4, 2, 9, 9, 9], 3, 15), (vec![4, 4, 4], 3, 0)]; |
| 64 | + println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len()); |
| 65 | + let mut success = 0; |
| 66 | + for (i, t) in tests.iter().enumerate() { |
| 67 | + let res = Solution::maximum_subarray_sum(t.0.clone(), t.1); |
| 68 | + if res == t.2 { |
| 69 | + success += 1; |
| 70 | + println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i); |
| 71 | + } else { |
| 72 | + println!( |
| 73 | + "\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {:?}!!\x1b[0m", |
| 74 | + i, t.2, res |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + println!(); |
| 79 | + if success == tests.len() { |
| 80 | + println!("\x1b[30;42m✔ All tests passed!\x1b[0m") |
| 81 | + } else if success == 0 { |
| 82 | + println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m") |
| 83 | + } else { |
| 84 | + println!( |
| 85 | + "\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", |
| 86 | + tests.len() - success |
| 87 | + ) |
| 88 | + } |
| 89 | +} |
0 commit comments