Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3257445

Browse files
feat: add solutions to lc problem: No.3631 (#4611)
No.3631.Sort Threats by Severity and Exploitability
1 parent 89adcfc commit 3257445

File tree

8 files changed

+229
-8
lines changed

8 files changed

+229
-8
lines changed

‎solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,109 @@ tags:
148148

149149
<!-- solution:start -->
150150

151-
### 方法一
151+
### 方法一:排序
152+
153+
我们直接按照题目要求的方式对数组进行排序即可。需要注意的是,分数是一个长整型数,因此在比较时需要使用长整型来避免溢出。
154+
155+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{threats}$ 的长度。
152156

153157
<!-- tabs:start -->
154158

155159
#### Python3
156160

157161
```python
158-
162+
class Solution:
163+
def sortThreats(self, threats: List[List[int]]) -> List[List[int]]:
164+
threats.sort(key=lambda x: (-(x[1] * 2 + x[2]), x[0]))
165+
return threats
159166
```
160167

161168
#### Java
162169

163170
```java
164-
171+
class Solution {
172+
public int[][] sortThreats(int[][] threats) {
173+
Arrays.sort(threats, (a, b) -> {
174+
long score1 = 2L * a[1] + a[2];
175+
long score2 = 2L * b[1] + b[2];
176+
if (score1 == score2) {
177+
return Integer.compare(a[0], b[0]);
178+
}
179+
return Long.compare(score2, score1);
180+
});
181+
return threats;
182+
}
183+
}
165184
```
166185

167186
#### C++
168187

169188
```cpp
170-
189+
class Solution {
190+
public:
191+
vector<vector<int>> sortThreats(vector<vector<int>>& threats) {
192+
sort(threats.begin(), threats.end(), [](const vector<int>& a, const vector<int>& b) {
193+
long long score1 = 2LL * a[1] + a[2];
194+
long long score2 = 2LL * b[1] + b[2];
195+
if (score1 == score2) {
196+
return a[0] < b[0];
197+
}
198+
return score2 < score1;
199+
});
200+
return threats;
201+
}
202+
};
171203
```
172204
173205
#### Go
174206
175207
```go
208+
func sortThreats(threats [][]int) [][]int {
209+
sort.Slice(threats, func(i, j int) bool {
210+
score1 := 2*int64(threats[i][1]) + int64(threats[i][2])
211+
score2 := 2*int64(threats[j][1]) + int64(threats[j][2])
212+
if score1 == score2 {
213+
return threats[i][0] < threats[j][0]
214+
}
215+
return score2 < score1
216+
})
217+
return threats
218+
}
219+
```
220+
221+
#### TypeScript
222+
223+
```ts
224+
function sortThreats(threats: number[][]): number[][] {
225+
threats.sort((a, b) => {
226+
const score1 = 2 * a[1] + a[2];
227+
const score2 = 2 * b[1] + b[2];
228+
if (score1 === score2) {
229+
return a[0] - b[0];
230+
}
231+
return score2 - score1;
232+
});
233+
return threats;
234+
}
235+
```
176236

237+
#### Rust
238+
239+
```rust
240+
impl Solution {
241+
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
242+
threats.sort_by(|a, b| {
243+
let score1 = 2i64 * a[1] as i64 + a[2] as i64;
244+
let score2 = 2i64 * b[1] as i64 + b[2] as i64;
245+
if score1 == score2 {
246+
a[0].cmp(&b[0])
247+
} else {
248+
score2.cmp(&score1)
249+
}
250+
});
251+
threats
252+
}
253+
}
177254
```
178255

179256
<!-- tabs:end -->

‎solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README_EN.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +146,109 @@ tags:
146146

147147
<!-- solution:start -->
148148

149-
### Solution 1
149+
### Solution 1: Sorting
150+
151+
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.
152+
153+
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}$.
150154

151155
<!-- tabs:start -->
152156

153157
#### Python3
154158

155159
```python
156-
160+
class Solution:
161+
def sortThreats(self, threats: List[List[int]]) -> List[List[int]]:
162+
threats.sort(key=lambda x: (-(x[1] * 2 + x[2]), x[0]))
163+
return threats
157164
```
158165

159166
#### Java
160167

161168
```java
162-
169+
class Solution {
170+
public int[][] sortThreats(int[][] threats) {
171+
Arrays.sort(threats, (a, b) -> {
172+
long score1 = 2L * a[1] + a[2];
173+
long score2 = 2L * b[1] + b[2];
174+
if (score1 == score2) {
175+
return Integer.compare(a[0], b[0]);
176+
}
177+
return Long.compare(score2, score1);
178+
});
179+
return threats;
180+
}
181+
}
163182
```
164183

165184
#### C++
166185

167186
```cpp
168-
187+
class Solution {
188+
public:
189+
vector<vector<int>> sortThreats(vector<vector<int>>& threats) {
190+
sort(threats.begin(), threats.end(), [](const vector<int>& a, const vector<int>& b) {
191+
long long score1 = 2LL * a[1] + a[2];
192+
long long score2 = 2LL * b[1] + b[2];
193+
if (score1 == score2) {
194+
return a[0] < b[0];
195+
}
196+
return score2 < score1;
197+
});
198+
return threats;
199+
}
200+
};
169201
```
170202
171203
#### Go
172204
173205
```go
206+
func sortThreats(threats [][]int) [][]int {
207+
sort.Slice(threats, func(i, j int) bool {
208+
score1 := 2*int64(threats[i][1]) + int64(threats[i][2])
209+
score2 := 2*int64(threats[j][1]) + int64(threats[j][2])
210+
if score1 == score2 {
211+
return threats[i][0] < threats[j][0]
212+
}
213+
return score2 < score1
214+
})
215+
return threats
216+
}
217+
```
218+
219+
#### TypeScript
220+
221+
```ts
222+
function sortThreats(threats: number[][]): number[][] {
223+
threats.sort((a, b) => {
224+
const score1 = 2 * a[1] + a[2];
225+
const score2 = 2 * b[1] + b[2];
226+
if (score1 === score2) {
227+
return a[0] - b[0];
228+
}
229+
return score2 - score1;
230+
});
231+
return threats;
232+
}
233+
```
174234

235+
#### Rust
236+
237+
```rust
238+
impl Solution {
239+
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
240+
threats.sort_by(|a, b| {
241+
let score1 = 2i64 * a[1] as i64 + a[2] as i64;
242+
let score2 = 2i64 * b[1] as i64 + b[2] as i64;
243+
if score1 == score2 {
244+
a[0].cmp(&b[0])
245+
} else {
246+
score2.cmp(&score1)
247+
}
248+
});
249+
threats
250+
}
251+
}
175252
```
176253

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /