Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problems: No.2678~2683 #3121

Merged
merged 1 commit into from
Jun 18, 2024
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
47 changes: 1 addition & 46 deletions solution/2600-2699/2678.Number of Senior Citizens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,52 +135,7 @@ func countSeniors(details []string) (ans int) {

```ts
function countSeniors(details: string[]): number {
let ans = 0;
for (const x of details) {
const age = parseInt(x.slice(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
let mut ans = 0;

for s in details.iter() {
if let Ok(age) = s[11..13].parse::<i32>() {
if age > 60 {
ans += 1;
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### TypeScript

```ts
function countSeniors(details: string[]): number {
return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
return details.filter(x => +x.slice(11, 13) > 60).length;
}
```

Expand Down
47 changes: 1 addition & 46 deletions solution/2600-2699/2678.Number of Senior Citizens/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,52 +133,7 @@ func countSeniors(details []string) (ans int) {

```ts
function countSeniors(details: string[]): number {
let ans = 0;
for (const x of details) {
const age = parseInt(x.slice(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
let mut ans = 0;

for s in details.iter() {
if let Ok(age) = s[11..13].parse::<i32>() {
if age > 60 {
ans += 1;
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function countSeniors(details: string[]): number {
return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
return details.filter(x => +x.slice(11, 13) > 60).length;
}
```

Expand Down
16 changes: 5 additions & 11 deletions solution/2600-2699/2678.Number of Senior Citizens/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
let mut ans = 0;

for s in details.iter() {
if let Ok(age) = s[11..13].parse::<i32>() {
if age > 60 {
ans += 1;
}
}
}

ans
details
.iter()
.filter_map(|s| s[11..13].parse::<i32>().ok())
.filter(|&age| age > 60)
.count() as i32
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
function countSeniors(details: string[]): number {
let ans = 0;
for (const x of details) {
const age = parseInt(x.slice(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
return details.filter(x => +x.slice(11, 13) > 60).length;
}

This file was deleted.

This file was deleted.

58 changes: 10 additions & 48 deletions solution/2600-2699/2679.Sum in a Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,18 @@ function matrixSum(nums: number[][]): number {

```rust
impl Solution {
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums;
for row in nums.iter_mut() {
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
for row in &mut nums {
row.sort();
}
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
.map(|i| {
nums.iter()
.map(|row| row[i])
.collect()
})
.collect();

transposed
.iter()
.map(|row| row.iter().max().unwrap())
(0..nums[0].len())
.map(|col|
nums
.iter()
.map(|row| row[col])
.max()
.unwrap()
)
.sum()
}
}
Expand All @@ -193,38 +189,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums.clone();
for row in nums.iter_mut() {
row.sort();
}

let mut ans = 0;
for j in 0..nums[0].len() {
let mut mx = 0;
for row in &nums {
mx = mx.max(row[j]);
}
ans += mx;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
58 changes: 10 additions & 48 deletions solution/2600-2699/2679.Sum in a Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,18 @@ function matrixSum(nums: number[][]): number {

```rust
impl Solution {
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums;
for row in nums.iter_mut() {
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
for row in &mut nums {
row.sort();
}
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
.map(|i| {
nums.iter()
.map(|row| row[i])
.collect()
})
.collect();

transposed
.iter()
.map(|row| row.iter().max().unwrap())
(0..nums[0].len())
.map(|col|
nums
.iter()
.map(|row| row[col])
.max()
.unwrap()
)
.sum()
}
}
Expand All @@ -184,38 +180,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums.clone();
for row in nums.iter_mut() {
row.sort();
}

let mut ans = 0;
for j in 0..nums[0].len() {
let mut mx = 0;
for row in &nums {
mx = mx.max(row[j]);
}
ans += mx;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
24 changes: 10 additions & 14 deletions solution/2600-2699/2679.Sum in a Matrix/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
impl Solution {
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums;
for row in nums.iter_mut() {
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
for row in &mut nums {
row.sort();
}
let transposed: Vec<Vec<i32>> = (0..nums[0].len())
.map(|i| {
nums.iter()
.map(|row| row[i])
.collect()
})
.collect();

transposed
.iter()
.map(|row| row.iter().max().unwrap())
(0..nums[0].len())
.map(|col|
nums
.iter()
.map(|row| row[col])
.max()
.unwrap()
)
.sum()
}
}
19 changes: 0 additions & 19 deletions solution/2600-2699/2679.Sum in a Matrix/Solution2.rs

This file was deleted.

18 changes: 17 additions & 1 deletion solution/2600-2699/2681.Power of Heroes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,23 @@ The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Mathematics

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.

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:

$$
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)
$$

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$.

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$.

After enumerating all $a_i$, return the answer.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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.

Finally, we iterate through the array `vis` and add the numbers of friends who have not received the ball to the answer array.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of friends.

<!-- tabs:start -->

Expand Down
Loading
Loading