Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions solution/3400-3499/3484.Design Spreadsheet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,44 @@ class Spreadsheet {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct Spreadsheet {
d: HashMap<String, i32>,
}

impl Spreadsheet {
fn new(_rows: i32) -> Self {
Spreadsheet {
d: HashMap::new(),
}
}

fn set_cell(&mut self, cell: String, value: i32) {
self.d.insert(cell, value);
}

fn reset_cell(&mut self, cell: String) {
self.d.remove(&cell);
}

fn get_value(&self, formula: String) -> i32 {
let mut ans = 0;
for cell in formula[1..].split('+') {
if cell.chars().next().unwrap().is_ascii_digit() {
ans += cell.parse::<i32>().unwrap();
} else {
ans += *self.d.get(cell).unwrap_or(&0);
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
38 changes: 38 additions & 0 deletions solution/3400-3499/3484.Design Spreadsheet/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ class Spreadsheet {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct Spreadsheet {
d: HashMap<String, i32>,
}

impl Spreadsheet {
fn new(_rows: i32) -> Self {
Spreadsheet {
d: HashMap::new(),
}
}

fn set_cell(&mut self, cell: String, value: i32) {
self.d.insert(cell, value);
}

fn reset_cell(&mut self, cell: String) {
self.d.remove(&cell);
}

fn get_value(&self, formula: String) -> i32 {
let mut ans = 0;
for cell in formula[1..].split('+') {
if cell.chars().next().unwrap().is_ascii_digit() {
ans += cell.parse::<i32>().unwrap();
} else {
ans += *self.d.get(cell).unwrap_or(&0);
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
31 changes: 31 additions & 0 deletions solution/3400-3499/3484.Design Spreadsheet/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::HashMap;

struct Spreadsheet {
d: HashMap<String, i32>,
}

impl Spreadsheet {
fn new(_rows: i32) -> Self {
Spreadsheet { d: HashMap::new() }
}

fn set_cell(&mut self, cell: String, value: i32) {
self.d.insert(cell, value);
}

fn reset_cell(&mut self, cell: String) {
self.d.remove(&cell);
}

fn get_value(&self, formula: String) -> i32 {
let mut ans = 0;
for cell in formula[1..].split('+') {
if cell.chars().next().unwrap().is_ascii_digit() {
ans += cell.parse::<i32>().unwrap();
} else {
ans += *self.d.get(cell).unwrap_or(&0);
}
}
ans
}
}
219 changes: 219 additions & 0 deletions solution/3600-3699/3672.Sum of Weighted Modes in Subarrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3672.Sum%20of%20Weighted%20Modes%20in%20Subarrays/README.md
---

<!-- problem:start -->

# [3672. Sum of Weighted Modes in Subarrays 🔒](https://leetcode.cn/problems/sum-of-weighted-modes-in-subarrays)

[English Version](/solution/3600-3699/3672.Sum%20of%20Weighted%20Modes%20in%20Subarrays/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>For every <strong>subarray</strong> of length <code>k</code>:</p>

<ul>
<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>
<li>The <strong>weight</strong> is defined as <code>mode * frequency(mode)</code>.</li>
</ul>

<p>Return the <strong>sum</strong> of the weights of all <strong>subarrays</strong> of length <code>k</code>.</p>

<p><strong>Note:</strong></p>

<ul>
<li>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</li>
<li>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in the array.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3], k = 3</span></p>

<p><strong>Output:</strong> <span class="example-io">8</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays of length <code>k = 3</code> are:</p>

<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Subarray</th>
<th style="border: 1px solid black;">Frequencies</th>
<th style="border: 1px solid black;">Mode</th>
<th style="border: 1px solid black;">Mode<br />
​​​​​​​Frequency</th>
<th style="border: 1px solid black;">Weight</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">[1, 2, 2]</td>
<td style="border: 1px solid black;">1: 1, 2: 2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2 &times; 2 = 4</td>
</tr>
<tr>
<td style="border: 1px solid black;">[2, 2, 3]</td>
<td style="border: 1px solid black;">2: 2, 3: 1</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2 &times; 2 = 4</td>
</tr>
</tbody>
</table>

<p>Thus, the sum of weights is <code>4 + 4 = 8</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,2], k = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays of length <code>k = 2</code> are:</p>

<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Subarray</th>
<th style="border: 1px solid black;">Frequencies</th>
<th style="border: 1px solid black;">Mode</th>
<th style="border: 1px solid black;">Mode<br />
Frequency</th>
<th style="border: 1px solid black;">Weight</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">1: 1, 2: 1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
</tr>
<tr>
<td style="border: 1px solid black;">[2, 1]</td>
<td style="border: 1px solid black;">2: 1, 1: 1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
</tr>
<tr>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">1: 1, 2: 1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
</tr>
</tbody>
</table>

<p>Thus, the sum of weights is <code>1 + 1 + 1 = 3</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,4,3], k = 3</span></p>

<p><strong>Output:</strong> <span class="example-io">14</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays of length <code>k = 3</code> are:</p>

<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Subarray</th>
<th style="border: 1px solid black;">Frequencies</th>
<th style="border: 1px solid black;">Mode</th>
<th style="border: 1px solid black;">Mode<br />
Frequency</th>
<th style="border: 1px solid black;">Weight</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">[4, 3, 4]</td>
<td style="border: 1px solid black;">4: 2, 3: 1</td>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2 &times; 4 = 8</td>
</tr>
<tr>
<td style="border: 1px solid black;">[3, 4, 3]</td>
<td style="border: 1px solid black;">3: 2, 4: 1</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2 &times; 3 = 6</td>
</tr>
</tbody>
</table>

<p>Thus, the sum of weights is <code>8 + 6 = 14</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading