3318. Find X-Sum of All K-Long Subarrays I Easy
1/**
2 * [3318] Find X-Sum of All K-Long Subarrays I
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 * 1 <= n == nums.length <= 50
35 * 1 <= nums[i] <= 50
36 * 1 <= x <= k <= nums.length
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/
42// discuss: https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn find_x_sum(nums: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
48 vec![]
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_3318() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.