Skip to content

Commit 9458d68

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 3318
1 parent dc7fb6e commit 9458d68

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@
307307
- [3005 Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/description/)
308308
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
309309
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
310+
- [3318 Find X-Sum of All K-Long Subarrays I](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/description/)
310311
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
311312
- [3408 Design Task Manager](https://leetcode.com/problems/design-task-manager/description/)
312313
- [3446 Sort Matrix by Diagonals](https://leetcode.com/problems/sort-matrix-by-diagonals/description/)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import collections
2+
import heapq
3+
from typing import List
4+
5+
6+
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
9+
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
10+
"""
11+
You are given an array nums of n integers and two integers k and x.
12+
13+
The x-sum of an array is calculated by the following procedure:
14+
- Count the occurrences of all elements in the array.
15+
- Keep only the occurrences of the top x most frequent elements. If two elements
16+
have the same number of occurrences, the element with the bigger value is
17+
considered more frequent.
18+
- Calculate the sum of the resulting array.
19+
20+
Note that if an array has less than x distinct elements, its x-sum is the sum of
21+
the array.
22+
23+
Return an integer array answer of length n - k + 1 where answer[i] is the x-sum
24+
of the subarray nums[i..i + k - 1].
25+
"""
26+
count = collections.defaultdict(int)
27+
prev = None
28+
res = []
29+
for i in range(len(nums) - k + 1):
30+
# Get the sliding window
31+
window = nums[i : i + k]
32+
33+
# Update count table
34+
if i == 0:
35+
# Compute count table for first time
36+
for val in window[:-1]:
37+
count[val] += 1
38+
39+
# Remove prev from count table
40+
if prev:
41+
count[prev] -= 1
42+
prev = window[0]
43+
44+
# Add newest into count table
45+
count[window[-1]] += 1
46+
47+
# Get the top-x values with their occurences
48+
top_x = heapq.nlargest(x, count.items(), key=lambda x: (x[1], x[0]))
49+
50+
# Compute the x-sum
51+
sum_x = sum(map(lambda x: x[0] * x[1], top_x))
52+
res.append(sum_x)
53+
54+
return res
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._3318_find_x_sum_of_all_k_long_subarrays_I import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "k", "x", "expected"],
10+
argvalues=[
11+
([1, 1, 2, 2, 3, 4, 2, 3], 6, 2, [6, 10, 12]),
12+
([3, 8, 7, 8, 7, 5], 2, 2, [11, 15, 15, 15, 12]),
13+
],
14+
)
15+
def test_func(nums: List[int], k: int, x: int, expected: List[int]):
16+
"""Tests the solution of a LeetCode problem."""
17+
x_sums = Solution().findXSum(nums, k, x)
18+
assert x_sums == expected

0 commit comments

Comments
 (0)