Skip to content

Commit 8cea261

Browse files
authored
feat: update solutions to lc problems: No.2678~2683 (#3121)
1 parent 534741b commit 8cea261

File tree

24 files changed

+121
-544
lines changed

24 files changed

+121
-544
lines changed

solution/2600-2699/2678.Number of Senior Citizens/README.md

+1-46
Original file line numberDiff line numberDiff line change
@@ -135,52 +135,7 @@ func countSeniors(details []string) (ans int) {
135135

136136
```ts
137137
function countSeniors(details: string[]): number {
138-
let ans = 0;
139-
for (const x of details) {
140-
const age = parseInt(x.slice(11, 13));
141-
if (age > 60) {
142-
++ans;
143-
}
144-
}
145-
return ans;
146-
}
147-
```
148-
149-
#### Rust
150-
151-
```rust
152-
impl Solution {
153-
pub fn count_seniors(details: Vec<String>) -> i32 {
154-
let mut ans = 0;
155-
156-
for s in details.iter() {
157-
if let Ok(age) = s[11..13].parse::<i32>() {
158-
if age > 60 {
159-
ans += 1;
160-
}
161-
}
162-
}
163-
164-
ans
165-
}
166-
}
167-
```
168-
169-
<!-- tabs:end -->
170-
171-
<!-- solution:end -->
172-
173-
<!-- solution:start -->
174-
175-
### 方法二
176-
177-
<!-- tabs:start -->
178-
179-
#### TypeScript
180-
181-
```ts
182-
function countSeniors(details: string[]): number {
183-
return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
138+
return details.filter(x => +x.slice(11, 13) > 60).length;
184139
}
185140
```
186141

solution/2600-2699/2678.Number of Senior Citizens/README_EN.md

+1-46
Original file line numberDiff line numberDiff line change
@@ -133,52 +133,7 @@ func countSeniors(details []string) (ans int) {
133133

134134
```ts
135135
function countSeniors(details: string[]): number {
136-
let ans = 0;
137-
for (const x of details) {
138-
const age = parseInt(x.slice(11, 13));
139-
if (age > 60) {
140-
++ans;
141-
}
142-
}
143-
return ans;
144-
}
145-
```
146-
147-
#### Rust
148-
149-
```rust
150-
impl Solution {
151-
pub fn count_seniors(details: Vec<String>) -> i32 {
152-
let mut ans = 0;
153-
154-
for s in details.iter() {
155-
if let Ok(age) = s[11..13].parse::<i32>() {
156-
if age > 60 {
157-
ans += 1;
158-
}
159-
}
160-
}
161-
162-
ans
163-
}
164-
}
165-
```
166-
167-
<!-- tabs:end -->
168-
169-
<!-- solution:end -->
170-
171-
<!-- solution:start -->
172-
173-
### Solution 2
174-
175-
<!-- tabs:start -->
176-
177-
#### TypeScript
178-
179-
```ts
180-
function countSeniors(details: string[]): number {
181-
return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
136+
return details.filter(x => +x.slice(11, 13) > 60).length;
182137
}
183138
```
184139

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
impl Solution {
22
pub fn count_seniors(details: Vec<String>) -> i32 {
3-
let mut ans = 0;
4-
5-
for s in details.iter() {
6-
if let Ok(age) = s[11..13].parse::<i32>() {
7-
if age > 60 {
8-
ans += 1;
9-
}
10-
}
11-
}
12-
13-
ans
3+
details
4+
.iter()
5+
.filter_map(|s| s[11..13].parse::<i32>().ok())
6+
.filter(|&age| age > 60)
7+
.count() as i32
148
}
159
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
function countSeniors(details: string[]): number {
2-
let ans = 0;
3-
for (const x of details) {
4-
const age = parseInt(x.slice(11, 13));
5-
if (age > 60) {
6-
++ans;
7-
}
8-
}
9-
return ans;
2+
return details.filter(x => +x.slice(11, 13) > 60).length;
103
}

solution/2600-2699/2678.Number of Senior Citizens/Solution2.rs

-9
This file was deleted.

solution/2600-2699/2678.Number of Senior Citizens/Solution2.ts

-3
This file was deleted.

solution/2600-2699/2679.Sum in a Matrix/README.md

+10-48
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,18 @@ function matrixSum(nums: number[][]): number {
168168

169169
```rust
170170
impl Solution {
171-
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
172-
let mut nums = nums;
173-
for row in nums.iter_mut() {
171+
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
172+
for row in &mut nums {
174173
row.sort();
175174
}
176-
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
177-
.map(|i| {
178-
nums.iter()
179-
.map(|row| row[i])
180-
.collect()
181-
})
182-
.collect();
183-
184-
transposed
185-
.iter()
186-
.map(|row| row.iter().max().unwrap())
175+
(0..nums[0].len())
176+
.map(|col|
177+
nums
178+
.iter()
179+
.map(|row| row[col])
180+
.max()
181+
.unwrap()
182+
)
187183
.sum()
188184
}
189185
}
@@ -193,38 +189,4 @@ impl Solution {
193189

194190
<!-- solution:end -->
195191

196-
<!-- solution:start -->
197-
198-
### 方法二
199-
200-
<!-- tabs:start -->
201-
202-
#### Rust
203-
204-
```rust
205-
impl Solution {
206-
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
207-
let mut nums = nums.clone();
208-
for row in nums.iter_mut() {
209-
row.sort();
210-
}
211-
212-
let mut ans = 0;
213-
for j in 0..nums[0].len() {
214-
let mut mx = 0;
215-
for row in &nums {
216-
mx = mx.max(row[j]);
217-
}
218-
ans += mx;
219-
}
220-
221-
ans
222-
}
223-
}
224-
```
225-
226-
<!-- tabs:end -->
227-
228-
<!-- solution:end -->
229-
230192
<!-- problem:end -->

solution/2600-2699/2679.Sum in a Matrix/README_EN.md

+10-48
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,18 @@ function matrixSum(nums: number[][]): number {
159159

160160
```rust
161161
impl Solution {
162-
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
163-
let mut nums = nums;
164-
for row in nums.iter_mut() {
162+
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
163+
for row in &mut nums {
165164
row.sort();
166165
}
167-
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
168-
.map(|i| {
169-
nums.iter()
170-
.map(|row| row[i])
171-
.collect()
172-
})
173-
.collect();
174-
175-
transposed
176-
.iter()
177-
.map(|row| row.iter().max().unwrap())
166+
(0..nums[0].len())
167+
.map(|col|
168+
nums
169+
.iter()
170+
.map(|row| row[col])
171+
.max()
172+
.unwrap()
173+
)
178174
.sum()
179175
}
180176
}
@@ -184,38 +180,4 @@ impl Solution {
184180

185181
<!-- solution:end -->
186182

187-
<!-- solution:start -->
188-
189-
### Solution 2
190-
191-
<!-- tabs:start -->
192-
193-
#### Rust
194-
195-
```rust
196-
impl Solution {
197-
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
198-
let mut nums = nums.clone();
199-
for row in nums.iter_mut() {
200-
row.sort();
201-
}
202-
203-
let mut ans = 0;
204-
for j in 0..nums[0].len() {
205-
let mut mx = 0;
206-
for row in &nums {
207-
mx = mx.max(row[j]);
208-
}
209-
ans += mx;
210-
}
211-
212-
ans
213-
}
214-
}
215-
```
216-
217-
<!-- tabs:end -->
218-
219-
<!-- solution:end -->
220-
221183
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
impl Solution {
2-
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
3-
let mut nums = nums;
4-
for row in nums.iter_mut() {
2+
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
3+
for row in &mut nums {
54
row.sort();
65
}
7-
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
8-
.map(|i| {
9-
nums.iter()
10-
.map(|row| row[i])
11-
.collect()
12-
})
13-
.collect();
14-
15-
transposed
16-
.iter()
17-
.map(|row| row.iter().max().unwrap())
6+
(0..nums[0].len())
7+
.map(|col|
8+
nums
9+
.iter()
10+
.map(|row| row[col])
11+
.max()
12+
.unwrap()
13+
)
1814
.sum()
1915
}
2016
}

solution/2600-2699/2679.Sum in a Matrix/Solution2.rs

-19
This file was deleted.

solution/2600-2699/2681.Power of Heroes/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,23 @@ The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Sorting + Mathematics
74+
75+
We notice that the problem involves the maximum and minimum values of a subsequence, and the order of elements in the array does not affect the final result. Therefore, we can sort the array first.
76+
77+
Next, we enumerate each element as the minimum value of the subsequence. Let's denote each element of the array as $a_1, a_2, \cdots, a_n$. The contribution of the subsequence with $a_i$ as the minimum value is:
78+
79+
$$
80+
a_i \times (a_{i}^{2} + a_{i+1}^2 + 2 \times a_{i+2}^2 + 4 \times a_{i+3}^2 + \cdots + 2^{n-i-1} \times a_n^2)
81+
$$
82+
83+
We notice that each $a_i$ will be multiplied by $a_i^2$, which we can directly add to the answer. For the remaining part, we can maintain it with a variable $p$, initially set to $0$.
84+
85+
Then, we enumerate $a_i$ from right to left. Each time, we add $a_i \times p$ to the answer, and then set $p = p \times 2 + a_i^2$.
86+
87+
After enumerating all $a_i$, return the answer.
88+
89+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.
7490

7591
<!-- tabs:start -->
7692

solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ tags:
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Simulation
80+
81+
We use an array `vis` to record whether each friend has received the ball, initially, all friends have not received the ball. Then, we simulate the game process according to the rules described in the problem statement until a friend receives the ball for the second time.
82+
83+
In the simulation process, we use two variables $i$ and $p$ to represent the current friend holding the ball and the current passing step length, respectively. Initially, $i=0, p=1$, indicating the first friend receives the ball. Each time the ball is passed, we update $i$ to $(i+p \times k) \bmod n$, representing the next friend's number to receive the ball, and then update $p$ to $p+1$, representing the step length for the next pass. The game ends when a friend receives the ball for the second time.
84+
85+
Finally, we iterate through the array `vis` and add the numbers of friends who have not received the ball to the answer array.
86+
87+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of friends.
8088

8189
<!-- tabs:start -->
8290

0 commit comments

Comments
 (0)