2461. Maximum Sum of Distinct Subarrays With Length K Medium

@problem@discussion
#Array#Hash Table#Sliding Window



1/**
2 * [2461] Maximum Sum of Distinct Subarrays With Length K
3 *
4 * You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:
5 * 
6 * 	The length of the subarray is k, and
7 * 	All the elements of the subarray are distinct.
8 * 
9 * Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.
10 * A subarray is a contiguous non-empty sequence of elements within an array.
11 *  
12 * <strong class="example">Example 1:
13 * 
14 * Input: nums = [1,5,4,2,9,9,9], k = 3
15 * Output: 15
16 * Explanation: The subarrays of nums with length 3 are:
17 * - [1,5,4] which meets the requirements and has a sum of 10.
18 * - [5,4,2] which meets the requirements and has a sum of 11.
19 * - [4,2,9] which meets the requirements and has a sum of 15.
20 * - [2,9,9] which does not meet the requirements because the element 9 is repeated.
21 * - [9,9,9] which does not meet the requirements because the element 9 is repeated.
22 * We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
23 * 
24 * <strong class="example">Example 2:
25 * 
26 * Input: nums = [4,4,4], k = 3
27 * Output: 0
28 * Explanation: The subarrays of nums with length 3 are:
29 * - [4,4,4] which does not meet the requirements because the element 4 is repeated.
30 * We return 0 because no subarrays meet the conditions.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= k <= nums.length <= 10^5
36 * 	1 <= nums[i] <= 10^5
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/
42// discuss: https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn maximum_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {
48        
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2461() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.