3321. Find X-Sum of All K-Long Subarrays II Hard
1/**
2 * [3321] Find X-Sum of All K-Long Subarrays II
3 *
4 * You are given an array nums of n integers and two integers k and x.
5 * The x-sum of an array is calculated by the following procedure:
6 *
7 * Count the occurrences of all elements in the array.
8 * Keep only the occurrences of the top x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.
9 * Calculate the sum of the resulting array.
10 *
11 * Note that if an array has less than x distinct elements, its x-sum is the sum of the array.
12 * Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the <span data-keyword="subarray-nonempty">subarray</span> nums[i..i + k - 1].
13 *
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [1,1,2,2,3,4,2,3], k = 6, x = 2</span>
17 * Output: <span class="example-io">[6,10,12]</span>
18 * Explanation:
19 *
20 * For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.
21 * For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.
22 * For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">nums = [3,8,7,8,7,5], k = 2, x = 2</span>
27 * Output: <span class="example-io">[11,15,15,15,12]</span>
28 * Explanation:
29 * Since k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].
30 * </div>
31 *
32 * Constraints:
33 *
34 * nums.length == n
35 * 1 <= n <= 10^5
36 * 1 <= nums[i] <= 10^9
37 * 1 <= x <= k <= nums.length
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii/
43// discuss: https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn find_x_sum(nums: Vec<i32>, k: i32, x: i32) -> Vec<i64> {
49
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3321() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.