Skip to content

Commit 8ef83d4

Browse files
committed
feat: add weekly contest 471
1 parent 566b1b1 commit 8ef83d4

File tree

19 files changed

+1337
-2
lines changed

19 files changed

+1337
-2
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3712. 出现次数能被 K 整除的元素总和](https://leetcode.cn/problems/sum-of-elements-with-frequency-divisible-by-k)
10+
11+
[English Version](/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code>。</p>
18+
19+
<p>请返回一个整数,表示 <code>nums</code> 中所有其 <strong>出现次数</strong> 能被 <code>k</code> 整除的元素的<strong>总和</strong>;如果没有这样的元素,则返回 0 。</p>
20+
21+
<p><strong>注意:</strong> 若某个元素在数组中的总出现次数能被 <code>k</code> 整除,则它在求和中会被计算 <strong>恰好</strong> 与其出现次数相同的次数。</p>
22+
23+
<p>元素 <code>x</code> 的&nbsp;<strong>出现次数&nbsp;</strong>指它在数组中出现的次数。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,2,3,3,3,3,4], k = 2</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">16</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<ul>
37+
<li>数字 1 出现 1 次(奇数次)。</li>
38+
<li>数字 2 出现 2 次(偶数次)。</li>
39+
<li>数字 3 出现 4 次(偶数次)。</li>
40+
<li>数字 4 出现 1 次(奇数次)。</li>
41+
</ul>
42+
43+
<p>因此总和为 <code>2 + 2 + 3 + 3 + 3 + 3 = 16</code>。</p>
44+
</div>
45+
46+
<p><strong class="example">示例 2:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3,4,5], k = 2</span></p>
50+
51+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
52+
53+
<p><strong>解释:</strong></p>
54+
55+
<p>没有元素出现偶数次,因此总和为 0。</p>
56+
</div>
57+
58+
<p><strong class="example">示例 3:</strong></p>
59+
60+
<div class="example-block">
61+
<p><strong>输入:</strong> <span class="example-io">nums = [4,4,4,1,2,3], k = 3</span></p>
62+
63+
<p><strong>输出:</strong> <span class="example-io">12</span></p>
64+
65+
<p><strong>解释:</strong></p>
66+
67+
<ul>
68+
<li>数字 1 出现 1 次。</li>
69+
<li>数字 2 出现 1 次。</li>
70+
<li>数字 3 出现 1 次。</li>
71+
<li>数字 4 出现 3 次。</li>
72+
</ul>
73+
74+
<p>因此总和为 <code>4 + 4 + 4 = 12</code>。</p>
75+
</div>
76+
77+
<p>&nbsp;</p>
78+
79+
<p><strong>提示:</strong></p>
80+
81+
<ul>
82+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
83+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
84+
<li><code>1 &lt;= k &lt;= 100</code></li>
85+
</ul>
86+
87+
<!-- description:end -->
88+
89+
## 解法
90+
91+
<!-- solution:start -->
92+
93+
### 方法一:计数
94+
95+
我们用一个哈希表 $\textit{cnt}$ 来记录每个数字的出现次数。遍历数组 $\textit{nums}$,对于每个数字 $x$,我们将 $\textit{cnt}[x]$ 增加 $1$。
96+
97+
然后,我们遍历哈希表 $\textit{cnt}$,对于每个元素 $x$,如果它的出现次数 $\textit{cnt}[x]$ 能被 $k$ 整除,就将 $x$ 乘以它的出现次数加到结果中。
98+
99+
时间复杂度为 $O(n)$,其中 $n$ 是数组的长度。空间复杂度为 $O(m)$,其中 $m$ 是数组中不同元素的数量。
100+
101+
<!-- tabs:start -->
102+
103+
#### Python3
104+
105+
```python
106+
class Solution:
107+
def sumDivisibleByK(self, nums: List[int], k: int) -> int:
108+
cnt = Counter(nums)
109+
return sum(x * v for x, v in cnt.items() if v % k == 0)
110+
```
111+
112+
#### Java
113+
114+
```java
115+
class Solution {
116+
public int sumDivisibleByK(int[] nums, int k) {
117+
Map<Integer, Integer> cnt = new HashMap<>();
118+
for (int x : nums) {
119+
cnt.merge(x, 1, Integer::sum);
120+
}
121+
int ans = 0;
122+
for (var e : cnt.entrySet()) {
123+
int x = e.getKey(), v = e.getValue();
124+
if (v % k == 0) {
125+
ans += x * v;
126+
}
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
133+
#### C++
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int sumDivisibleByK(vector<int>& nums, int k) {
139+
unordered_map<int, int> cnt;
140+
for (int x : nums) {
141+
++cnt[x];
142+
}
143+
int ans = 0;
144+
for (auto& [x, v] : cnt) {
145+
if (v % k == 0) {
146+
ans += x * v;
147+
}
148+
}
149+
return ans;
150+
}
151+
};
152+
```
153+
154+
#### Go
155+
156+
```go
157+
func sumDivisibleByK(nums []int, k int) (ans int) {
158+
cnt := map[int]int{}
159+
for _, x := range nums {
160+
cnt[x]++
161+
}
162+
for x, v := range cnt {
163+
if v%k == 0 {
164+
ans += x * v
165+
}
166+
}
167+
return
168+
}
169+
```
170+
171+
#### TypeScript
172+
173+
```ts
174+
function sumDivisibleByK(nums: number[], k: number): number {
175+
const cnt = new Map();
176+
for (const x of nums) {
177+
cnt.set(x, (cnt.get(x) || 0) + 1);
178+
}
179+
let ans = 0;
180+
for (const [x, v] of cnt.entries()) {
181+
if (v % k === 0) {
182+
ans += x * v;
183+
}
184+
}
185+
return ans;
186+
}
187+
```
188+
189+
<!-- tabs:end -->
190+
191+
<!-- solution:end -->
192+
193+
<!-- problem:end -->
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3712. Sum of Elements With Frequency Divisible by K](https://leetcode.com/problems/sum-of-elements-with-frequency-divisible-by-k)
10+
11+
[中文文档](/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
18+
19+
<p>Return an integer denoting the <strong>sum</strong> of all elements in <code>nums</code> whose <strong>frequency</strong> is divisible by <code>k</code>, or 0 if there are no such elements.</p>
20+
21+
<p><strong>Note:</strong> An element is included in the sum <strong>exactly</strong> as many times as it appears in the array if its total frequency is divisible by <code>k</code>.</p>
22+
23+
<p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in the array.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3,3,3,3,4], k = 2</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">16</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<ul>
36+
<li>The number 1 appears once (odd frequency).</li>
37+
<li>The number 2 appears twice (even frequency).</li>
38+
<li>The number 3 appears four times (even frequency).</li>
39+
<li>The number 4 appears once (odd frequency).</li>
40+
</ul>
41+
42+
<p>So, the total sum is <code>2 + 2 + 3 + 3 + 3 + 3 = 16</code>.</p>
43+
</div>
44+
45+
<p><strong class="example">Example 2:</strong></p>
46+
47+
<div class="example-block">
48+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5], k = 2</span></p>
49+
50+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
51+
52+
<p><strong>Explanation:</strong></p>
53+
54+
<p>There are no elements that appear an even number of times, so the total sum is 0.</p>
55+
</div>
56+
57+
<p><strong class="example">Example 3:</strong></p>
58+
59+
<div class="example-block">
60+
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,4,1,2,3], k = 3</span></p>
61+
62+
<p><strong>Output:</strong> <span class="example-io">12</span></p>
63+
64+
<p><strong>Explanation:</strong></p>
65+
66+
<ul>
67+
<li>The number 1 appears once.</li>
68+
<li>The number 2 appears once.</li>
69+
<li>The number 3 appears once.</li>
70+
<li>The number 4 appears three times.</li>
71+
</ul>
72+
73+
<p>So, the total sum is <code>4 + 4 + 4 = 12</code>.</p>
74+
</div>
75+
76+
<p>&nbsp;</p>
77+
<p><strong>Constraints:</strong></p>
78+
79+
<ul>
80+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
81+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
82+
<li><code>1 &lt;= k &lt;= 100</code></li>
83+
</ul>
84+
85+
<!-- description:end -->
86+
87+
## Solutions
88+
89+
<!-- solution:start -->
90+
91+
### Solution 1: Counting
92+
93+
We use a hash table $\textit{cnt}$ to record the frequency of each number. We traverse the array $\textit{nums}$, and for each number $x$, we increment $\textit{cnt}[x]$ by $1$.
94+
95+
Then, we traverse the hash table $\textit{cnt}$. For each element $x$, if its frequency $\textit{cnt}[x]$ is divisible by $k$, we add $x$ multiplied by its frequency to the result.
96+
97+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(m)$, where $m$ is the number of distinct elements in the array.
98+
99+
<!-- tabs:start -->
100+
101+
#### Python3
102+
103+
```python
104+
class Solution:
105+
def sumDivisibleByK(self, nums: List[int], k: int) -> int:
106+
cnt = Counter(nums)
107+
return sum(x * v for x, v in cnt.items() if v % k == 0)
108+
```
109+
110+
#### Java
111+
112+
```java
113+
class Solution {
114+
public int sumDivisibleByK(int[] nums, int k) {
115+
Map<Integer, Integer> cnt = new HashMap<>();
116+
for (int x : nums) {
117+
cnt.merge(x, 1, Integer::sum);
118+
}
119+
int ans = 0;
120+
for (var e : cnt.entrySet()) {
121+
int x = e.getKey(), v = e.getValue();
122+
if (v % k == 0) {
123+
ans += x * v;
124+
}
125+
}
126+
return ans;
127+
}
128+
}
129+
```
130+
131+
#### C++
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
int sumDivisibleByK(vector<int>& nums, int k) {
137+
unordered_map<int, int> cnt;
138+
for (int x : nums) {
139+
++cnt[x];
140+
}
141+
int ans = 0;
142+
for (auto& [x, v] : cnt) {
143+
if (v % k == 0) {
144+
ans += x * v;
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
151+
152+
#### Go
153+
154+
```go
155+
func sumDivisibleByK(nums []int, k int) (ans int) {
156+
cnt := map[int]int{}
157+
for _, x := range nums {
158+
cnt[x]++
159+
}
160+
for x, v := range cnt {
161+
if v%k == 0 {
162+
ans += x * v
163+
}
164+
}
165+
return
166+
}
167+
```
168+
169+
#### TypeScript
170+
171+
```ts
172+
function sumDivisibleByK(nums: number[], k: number): number {
173+
const cnt = new Map();
174+
for (const x of nums) {
175+
cnt.set(x, (cnt.get(x) || 0) + 1);
176+
}
177+
let ans = 0;
178+
for (const [x, v] of cnt.entries()) {
179+
if (v % k === 0) {
180+
ans += x * v;
181+
}
182+
}
183+
return ans;
184+
}
185+
```
186+
187+
<!-- tabs:end -->
188+
189+
<!-- solution:end -->
190+
191+
<!-- problem:end -->

0 commit comments

Comments
 (0)