Skip to content

feat: add solutions to lc problem: No.3631 #4611

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

Merged
merged 1 commit into from
Aug 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,109 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:排序

我们直接按照题目要求的方式对数组进行排序即可。需要注意的是,分数是一个长整型数,因此在比较时需要使用长整型来避免溢出。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{threats}$ 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def sortThreats(self, threats: List[List[int]]) -> List[List[int]]:
threats.sort(key=lambda x: (-(x[1] * 2 + x[2]), x[0]))
return threats
```

#### Java

```java

class Solution {
public int[][] sortThreats(int[][] threats) {
Arrays.sort(threats, (a, b) -> {
long score1 = 2L * a[1] + a[2];
long score2 = 2L * b[1] + b[2];
if (score1 == score2) {
return Integer.compare(a[0], b[0]);
}
return Long.compare(score2, score1);
});
return threats;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<vector<int>> sortThreats(vector<vector<int>>& threats) {
sort(threats.begin(), threats.end(), [](const vector<int>& a, const vector<int>& b) {
long long score1 = 2LL * a[1] + a[2];
long long score2 = 2LL * b[1] + b[2];
if (score1 == score2) {
return a[0] < b[0];
}
return score2 < score1;
});
return threats;
}
};
```

#### Go

```go
func sortThreats(threats [][]int) [][]int {
sort.Slice(threats, func(i, j int) bool {
score1 := 2*int64(threats[i][1]) + int64(threats[i][2])
score2 := 2*int64(threats[j][1]) + int64(threats[j][2])
if score1 == score2 {
return threats[i][0] < threats[j][0]
}
return score2 < score1
})
return threats
}
```

#### TypeScript

```ts
function sortThreats(threats: number[][]): number[][] {
threats.sort((a, b) => {
const score1 = 2 * a[1] + a[2];
const score2 = 2 * b[1] + b[2];
if (score1 === score2) {
return a[0] - b[0];
}
return score2 - score1;
});
return threats;
}
```

#### Rust

```rust
impl Solution {
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
threats.sort_by(|a, b| {
let score1 = 2i64 * a[1] as i64 + a[2] as i64;
let score2 = 2i64 * b[1] as i64 + b[2] as i64;
if score1 == score2 {
a[0].cmp(&b[0])
} else {
score2.cmp(&score1)
}
});
threats
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,109 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

We can directly sort the array according to the requirements of the problem. Note that the score is a long integer, so we need to use long integers when comparing to avoid overflow.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\text{threats}$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def sortThreats(self, threats: List[List[int]]) -> List[List[int]]:
threats.sort(key=lambda x: (-(x[1] * 2 + x[2]), x[0]))
return threats
```

#### Java

```java

class Solution {
public int[][] sortThreats(int[][] threats) {
Arrays.sort(threats, (a, b) -> {
long score1 = 2L * a[1] + a[2];
long score2 = 2L * b[1] + b[2];
if (score1 == score2) {
return Integer.compare(a[0], b[0]);
}
return Long.compare(score2, score1);
});
return threats;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<vector<int>> sortThreats(vector<vector<int>>& threats) {
sort(threats.begin(), threats.end(), [](const vector<int>& a, const vector<int>& b) {
long long score1 = 2LL * a[1] + a[2];
long long score2 = 2LL * b[1] + b[2];
if (score1 == score2) {
return a[0] < b[0];
}
return score2 < score1;
});
return threats;
}
};
```

#### Go

```go
func sortThreats(threats [][]int) [][]int {
sort.Slice(threats, func(i, j int) bool {
score1 := 2*int64(threats[i][1]) + int64(threats[i][2])
score2 := 2*int64(threats[j][1]) + int64(threats[j][2])
if score1 == score2 {
return threats[i][0] < threats[j][0]
}
return score2 < score1
})
return threats
}
```

#### TypeScript

```ts
function sortThreats(threats: number[][]): number[][] {
threats.sort((a, b) => {
const score1 = 2 * a[1] + a[2];
const score2 = 2 * b[1] + b[2];
if (score1 === score2) {
return a[0] - b[0];
}
return score2 - score1;
});
return threats;
}
```

#### Rust

```rust
impl Solution {
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
threats.sort_by(|a, b| {
let score1 = 2i64 * a[1] as i64 + a[2] as i64;
let score2 = 2i64 * b[1] as i64 + b[2] as i64;
if score1 == score2 {
a[0].cmp(&b[0])
} else {
score2.cmp(&score1)
}
});
threats
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
vector<vector<int>> sortThreats(vector<vector<int>>& threats) {
sort(threats.begin(), threats.end(), [](const vector<int>& a, const vector<int>& b) {
long long score1 = 2LL * a[1] + a[2];
long long score2 = 2LL * b[1] + b[2];
if (score1 == score2) {
return a[0] < b[0];
}
return score2 < score1;
});
return threats;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func sortThreats(threats [][]int) [][]int {
sort.Slice(threats, func(i, j int) bool {
score1 := 2*int64(threats[i][1]) + int64(threats[i][2])
score2 := 2*int64(threats[j][1]) + int64(threats[j][2])
if score1 == score2 {
return threats[i][0] < threats[j][0]
}
return score2 < score1
})
return threats
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int[][] sortThreats(int[][] threats) {
Arrays.sort(threats, (a, b) -> {
long score1 = 2L * a[1] + a[2];
long score2 = 2L * b[1] + b[2];
if (score1 == score2) {
return Integer.compare(a[0], b[0]);
}
return Long.compare(score2, score1);
});
return threats;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def sortThreats(self, threats: List[List[int]]) -> List[List[int]]:
threats.sort(key=lambda x: (-(x[1] * 2 + x[2]), x[0]))
return threats
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl Solution {
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
threats.sort_by(|a, b| {
let score1 = 2i64 * a[1] as i64 + a[2] as i64;
let score2 = 2i64 * b[1] as i64 + b[2] as i64;
if score1 == score2 {
a[0].cmp(&b[0])
} else {
score2.cmp(&score1)
}
});
threats
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function sortThreats(threats: number[][]): number[][] {
threats.sort((a, b) => {
const score1 = 2 * a[1] + a[2];
const score2 = 2 * b[1] + b[2];
if (score1 === score2) {
return a[0] - b[0];
}
return score2 - score1;
});
return threats;
}