Skip to content

Commit 29183b6

Browse files
authored
feat: add weekly contest 466 (#4701)
1 parent a07146e commit 29183b6

File tree

23 files changed

+1778
-2
lines changed

23 files changed

+1778
-2
lines changed

solution/3400-3499/3484.Design Spreadsheet/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,44 @@ class Spreadsheet {
274274
*/
275275
```
276276

277+
#### Rust
278+
279+
```rust
280+
use std::collections::HashMap;
281+
282+
struct Spreadsheet {
283+
d: HashMap<String, i32>,
284+
}
285+
286+
impl Spreadsheet {
287+
fn new(_rows: i32) -> Self {
288+
Spreadsheet {
289+
d: HashMap::new(),
290+
}
291+
}
292+
293+
fn set_cell(&mut self, cell: String, value: i32) {
294+
self.d.insert(cell, value);
295+
}
296+
297+
fn reset_cell(&mut self, cell: String) {
298+
self.d.remove(&cell);
299+
}
300+
301+
fn get_value(&self, formula: String) -> i32 {
302+
let mut ans = 0;
303+
for cell in formula[1..].split('+') {
304+
if cell.chars().next().unwrap().is_ascii_digit() {
305+
ans += cell.parse::<i32>().unwrap();
306+
} else {
307+
ans += *self.d.get(cell).unwrap_or(&0);
308+
}
309+
}
310+
ans
311+
}
312+
}
313+
```
314+
277315
<!-- tabs:end -->
278316

279317
<!-- solution:end -->

solution/3400-3499/3484.Design Spreadsheet/README_EN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,44 @@ class Spreadsheet {
272272
*/
273273
```
274274

275+
#### Rust
276+
277+
```rust
278+
use std::collections::HashMap;
279+
280+
struct Spreadsheet {
281+
d: HashMap<String, i32>,
282+
}
283+
284+
impl Spreadsheet {
285+
fn new(_rows: i32) -> Self {
286+
Spreadsheet {
287+
d: HashMap::new(),
288+
}
289+
}
290+
291+
fn set_cell(&mut self, cell: String, value: i32) {
292+
self.d.insert(cell, value);
293+
}
294+
295+
fn reset_cell(&mut self, cell: String) {
296+
self.d.remove(&cell);
297+
}
298+
299+
fn get_value(&self, formula: String) -> i32 {
300+
let mut ans = 0;
301+
for cell in formula[1..].split('+') {
302+
if cell.chars().next().unwrap().is_ascii_digit() {
303+
ans += cell.parse::<i32>().unwrap();
304+
} else {
305+
ans += *self.d.get(cell).unwrap_or(&0);
306+
}
307+
}
308+
ans
309+
}
310+
}
311+
```
312+
275313
<!-- tabs:end -->
276314

277315
<!-- solution:end -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::HashMap;
2+
3+
struct Spreadsheet {
4+
d: HashMap<String, i32>,
5+
}
6+
7+
impl Spreadsheet {
8+
fn new(_rows: i32) -> Self {
9+
Spreadsheet { d: HashMap::new() }
10+
}
11+
12+
fn set_cell(&mut self, cell: String, value: i32) {
13+
self.d.insert(cell, value);
14+
}
15+
16+
fn reset_cell(&mut self, cell: String) {
17+
self.d.remove(&cell);
18+
}
19+
20+
fn get_value(&self, formula: String) -> i32 {
21+
let mut ans = 0;
22+
for cell in formula[1..].split('+') {
23+
if cell.chars().next().unwrap().is_ascii_digit() {
24+
ans += cell.parse::<i32>().unwrap();
25+
} else {
26+
ans += *self.d.get(cell).unwrap_or(&0);
27+
}
28+
}
29+
ans
30+
}
31+
}
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3672.Sum%20of%20Weighted%20Modes%20in%20Subarrays/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3672. Sum of Weighted Modes in Subarrays 🔒](https://leetcode.cn/problems/sum-of-weighted-modes-in-subarrays)
10+
11+
[English Version](/solution/3600-3699/3672.Sum%20of%20Weighted%20Modes%20in%20Subarrays/README_EN.md)
12+
13+
## 题目描述
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>For every <strong>subarray</strong> of length <code>k</code>:</p>
20+
21+
<ul>
22+
<li>The <strong>mode</strong> is defined as the element with the <strong>highest frequency</strong>. If there are multiple choices for a mode, the <strong>smallest</strong> such element is taken.</li>
23+
<li>The <strong>weight</strong> is defined as <code>mode * frequency(mode)</code>.</li>
24+
</ul>
25+
26+
<p>Return the <strong>sum</strong> of the weights of all <strong>subarrays</strong> of length <code>k</code>.</p>
27+
28+
<p><strong>Note:</strong></p>
29+
30+
<ul>
31+
<li>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</li>
32+
<li>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in the array.</li>
33+
</ul>
34+
35+
<p>&nbsp;</p>
36+
<p><strong class="example">Example 1:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3], k = 3</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">8</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>Subarrays of length <code>k = 3</code> are:</p>
46+
47+
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
48+
<thead>
49+
<tr>
50+
<th style="border: 1px solid black;">Subarray</th>
51+
<th style="border: 1px solid black;">Frequencies</th>
52+
<th style="border: 1px solid black;">Mode</th>
53+
<th style="border: 1px solid black;">Mode<br />
54+
​​​​​​​Frequency</th>
55+
<th style="border: 1px solid black;">Weight</th>
56+
</tr>
57+
</thead>
58+
<tbody>
59+
<tr>
60+
<td style="border: 1px solid black;">[1, 2, 2]</td>
61+
<td style="border: 1px solid black;">1: 1, 2: 2</td>
62+
<td style="border: 1px solid black;">2</td>
63+
<td style="border: 1px solid black;">2</td>
64+
<td style="border: 1px solid black;">2 &times; 2 = 4</td>
65+
</tr>
66+
<tr>
67+
<td style="border: 1px solid black;">[2, 2, 3]</td>
68+
<td style="border: 1px solid black;">2: 2, 3: 1</td>
69+
<td style="border: 1px solid black;">2</td>
70+
<td style="border: 1px solid black;">2</td>
71+
<td style="border: 1px solid black;">2 &times; 2 = 4</td>
72+
</tr>
73+
</tbody>
74+
</table>
75+
76+
<p>Thus, the sum of weights is <code>4 + 4 = 8</code>.</p>
77+
</div>
78+
79+
<p><strong class="example">Example 2:</strong></p>
80+
81+
<div class="example-block">
82+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,2], k = 2</span></p>
83+
84+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
85+
86+
<p><strong>Explanation:</strong></p>
87+
88+
<p>Subarrays of length <code>k = 2</code> are:</p>
89+
90+
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
91+
<thead>
92+
<tr>
93+
<th style="border: 1px solid black;">Subarray</th>
94+
<th style="border: 1px solid black;">Frequencies</th>
95+
<th style="border: 1px solid black;">Mode</th>
96+
<th style="border: 1px solid black;">Mode<br />
97+
Frequency</th>
98+
<th style="border: 1px solid black;">Weight</th>
99+
</tr>
100+
</thead>
101+
<tbody>
102+
<tr>
103+
<td style="border: 1px solid black;">[1, 2]</td>
104+
<td style="border: 1px solid black;">1: 1, 2: 1</td>
105+
<td style="border: 1px solid black;">1</td>
106+
<td style="border: 1px solid black;">1</td>
107+
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
108+
</tr>
109+
<tr>
110+
<td style="border: 1px solid black;">[2, 1]</td>
111+
<td style="border: 1px solid black;">2: 1, 1: 1</td>
112+
<td style="border: 1px solid black;">1</td>
113+
<td style="border: 1px solid black;">1</td>
114+
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
115+
</tr>
116+
<tr>
117+
<td style="border: 1px solid black;">[1, 2]</td>
118+
<td style="border: 1px solid black;">1: 1, 2: 1</td>
119+
<td style="border: 1px solid black;">1</td>
120+
<td style="border: 1px solid black;">1</td>
121+
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
122+
</tr>
123+
</tbody>
124+
</table>
125+
126+
<p>Thus, the sum of weights is <code>1 + 1 + 1 = 3</code>.</p>
127+
</div>
128+
129+
<p><strong class="example">Example 3:</strong></p>
130+
131+
<div class="example-block">
132+
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,4,3], k = 3</span></p>
133+
134+
<p><strong>Output:</strong> <span class="example-io">14</span></p>
135+
136+
<p><strong>Explanation:</strong></p>
137+
138+
<p>Subarrays of length <code>k = 3</code> are:</p>
139+
140+
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
141+
<thead>
142+
<tr>
143+
<th style="border: 1px solid black;">Subarray</th>
144+
<th style="border: 1px solid black;">Frequencies</th>
145+
<th style="border: 1px solid black;">Mode</th>
146+
<th style="border: 1px solid black;">Mode<br />
147+
Frequency</th>
148+
<th style="border: 1px solid black;">Weight</th>
149+
</tr>
150+
</thead>
151+
<tbody>
152+
<tr>
153+
<td style="border: 1px solid black;">[4, 3, 4]</td>
154+
<td style="border: 1px solid black;">4: 2, 3: 1</td>
155+
<td style="border: 1px solid black;">4</td>
156+
<td style="border: 1px solid black;">2</td>
157+
<td style="border: 1px solid black;">2 &times; 4 = 8</td>
158+
</tr>
159+
<tr>
160+
<td style="border: 1px solid black;">[3, 4, 3]</td>
161+
<td style="border: 1px solid black;">3: 2, 4: 1</td>
162+
<td style="border: 1px solid black;">3</td>
163+
<td style="border: 1px solid black;">2</td>
164+
<td style="border: 1px solid black;">2 &times; 3 = 6</td>
165+
</tr>
166+
</tbody>
167+
</table>
168+
169+
<p>Thus, the sum of weights is <code>8 + 6 = 14</code>.</p>
170+
</div>
171+
172+
<p>&nbsp;</p>
173+
<p><strong>Constraints:</strong></p>
174+
175+
<ul>
176+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
177+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
178+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
179+
</ul>
180+
181+
<!-- description:end -->
182+
183+
## 解法
184+
185+
<!-- solution:start -->
186+
187+
### 方法一
188+
189+
<!-- tabs:start -->
190+
191+
#### Python3
192+
193+
```python
194+
195+
```
196+
197+
#### Java
198+
199+
```java
200+
201+
```
202+
203+
#### C++
204+
205+
```cpp
206+
207+
```
208+
209+
#### Go
210+
211+
```go
212+
213+
```
214+
215+
<!-- tabs:end -->
216+
217+
<!-- solution:end -->
218+
219+
<!-- problem:end -->

0 commit comments

Comments
 (0)