Skip to content

3610. Find X Sum Of All K Long Subarrays I

Array Hash Table Sliding Window Heap (Priority Queue)

Problem - Find X Sum Of All K Long Subarrays I

Easy

You are given an array nums of n integers and two integers k and x.

The x-sum of an array is calculated by the following procedure:

  • Count the occurrences of all elements in the array.
  • 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.
  • Calculate the sum of the resulting array.

Note that if an array has less than x distinct elements, its x-sum is the sum of the array.

Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].

 

Example 1:

Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2

Output: [6,10,12]

Explanation:

  • 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.
  • 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.
  • 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.

Example 2:

Input: nums = [3,8,7,8,7,5], k = 2, x = 2

Output: [11,15,15,15,12]

Explanation:

Since k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].

 

Constraints:

  • 1 <= n == nums.length <= 50
  • 1 <= nums[i] <= 50
  • 1 <= x <= k <= nums.length

Solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        # left = SortedList()
        # right = SortedList()
        # count = Counter()
        # sum_val = 0
        # n = len(nums)
        # result = [0] * (n - k + 1)

        # def add(val: int):
        #     if count[val] == 0:
        #         return
        #     temp = (count[val], val)
        #     if left and temp > left[0]:
        #         nonlocal sum_val
        #         sum_val += temp[0] * temp[1]
        #         left.add(temp)
        #     else:
        #         right.add(temp)

        # def remove(val: int):
        #     if count[val] == 0:
        #         return
        #     temp = (count[val], val)
        #     if temp in left:
        #         nonlocal sum_val
        #         sum_val -= temp[0] * temp[1]
        #         left.remove(temp)
        #     else:
        #         right.remove(temp)

        # for i, val in enumerate(nums):
        #     remove(val)
        #     count[val] += 1
        #     add(val)
        #     j = i - k + 1
        #     if j < 0:
        #         continue
        #     while right and len(left) < x:
        #         temp = right.pop()
        #         left.add(temp)
        #         sum_val += temp[0] * temp[1]

        #     while len(left) > x:
        #         temp = left.pop(0)
        #         sum_val -= temp[0] * temp[1]
        #         right.add(temp)
        #     result[j] = sum_val

        #     remove(nums[j])
        #     count[nums[j]] -= 1
        #     add(nums[j])

        # return result

        n = len(nums)
        i = 0
        result = []

        while i + k - 1 < n:
            sum_val = 0
            m = nums[i:i+k]
            visited = Counter(m)
            seen = sorted(visited.items(), key=lambda item: (item[1], item[0]))[-x:]
            if len(visited) < x:
                result.append(sum(m))
            else:
                for key, val in seen:
                    sum_val += key * val
                result.append(sum_val)
            i += 1
        return result

Submission Stats:

  • Runtime: 24 ms (62.86%)
  • Memory: 17.7 MB (96.88%)