#3631
Medium Algorithms Sort threats by severity and exploitability
Array Sorting
63.4% acceptance
Mar 31, 2026
5
1
You are given a 2D integer array threats, where each threats[i] = [IDi, sevi, expi]
IDi: Unique identifier of the threat.
sevi: Indicates the severity of the threat.
expi: Indicates the exploitability of the threat.
The score of a threat i is defined as: score = 2 × sevi + expi
Your task is to return threats sorted in descending order of score.
If multiple threats have the same score, sort them by ascending ID.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn sort_threats(mut threats: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
threats.sort_by(|a, b| {
let score_a = 2i64 * a[1] as i64 + a[2] as i64;
let score_b = 2i64 * b[1] as i64 + b[2] as i64;
score_b.cmp(&score_a).then(a[0].cmp(&b[0]))
});
threats
}
}