diff --git a/solution/2500-2599/2561.Rearranging Fruits/README.md b/solution/2500-2599/2561.Rearranging Fruits/README.md index f035e6bb3c6a3..ec80d1c864dc2 100644 --- a/solution/2500-2599/2561.Rearranging Fruits/README.md +++ b/solution/2500-2599/2561.Rearranging Fruits/README.md @@ -159,7 +159,7 @@ public: } mi = min(mi, x); } - sort(nums.begin(), nums.end()); + ranges::sort(nums); int m = nums.size(); long long ans = 0; for (int i = 0; i < m / 2; ++i) { @@ -206,6 +206,80 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function minCost(basket1: number[], basket2: number[]): number { + const n = basket1.length; + const cnt: Map = new Map(); + for (let i = 0; i < n; i++) { + cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1); + cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1); + } + let mi = Number.MAX_SAFE_INTEGER; + const nums: number[] = []; + for (const [x, v] of cnt.entries()) { + if (v % 2 !== 0) { + return -1; + } + for (let i = 0; i < Math.abs(v) / 2; i++) { + nums.push(x); + } + mi = Math.min(mi, x); + } + + nums.sort((a, b) => a - b); + const m = nums.length; + let ans = 0; + for (let i = 0; i < m / 2; i++) { + ans += Math.min(nums[i], mi * 2); + } + return ans; +} +``` + +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn min_cost(basket1: Vec, basket2: Vec) -> i64 { + let n = basket1.len(); + let mut cnt: HashMap = HashMap::new(); + + for i in 0..n { + *cnt.entry(basket1[i]).or_insert(0) += 1; + *cnt.entry(basket2[i]).or_insert(0) -= 1; + } + + let mut mi = i32::MAX; + let mut nums = Vec::new(); + + for (x, v) in cnt { + if v % 2 != 0 { + return -1; + } + for _ in 0..(v.abs() / 2) { + nums.push(x); + } + mi = mi.min(x); + } + + nums.sort(); + + let m = nums.len(); + let mut ans = 0; + + for i in 0..(m / 2) { + ans += nums[i].min(mi * 2) as i64; + } + + ans + } +} +``` + diff --git a/solution/2500-2599/2561.Rearranging Fruits/README_EN.md b/solution/2500-2599/2561.Rearranging Fruits/README_EN.md index 48b1fbd172ccc..47446a99f532e 100644 --- a/solution/2500-2599/2561.Rearranging Fruits/README_EN.md +++ b/solution/2500-2599/2561.Rearranging Fruits/README_EN.md @@ -157,7 +157,7 @@ public: } mi = min(mi, x); } - sort(nums.begin(), nums.end()); + ranges::sort(nums); int m = nums.size(); long long ans = 0; for (int i = 0; i < m / 2; ++i) { @@ -204,6 +204,80 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function minCost(basket1: number[], basket2: number[]): number { + const n = basket1.length; + const cnt: Map = new Map(); + for (let i = 0; i < n; i++) { + cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1); + cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1); + } + let mi = Number.MAX_SAFE_INTEGER; + const nums: number[] = []; + for (const [x, v] of cnt.entries()) { + if (v % 2 !== 0) { + return -1; + } + for (let i = 0; i < Math.abs(v) / 2; i++) { + nums.push(x); + } + mi = Math.min(mi, x); + } + + nums.sort((a, b) => a - b); + const m = nums.length; + let ans = 0; + for (let i = 0; i < m / 2; i++) { + ans += Math.min(nums[i], mi * 2); + } + return ans; +} +``` + +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn min_cost(basket1: Vec, basket2: Vec) -> i64 { + let n = basket1.len(); + let mut cnt: HashMap = HashMap::new(); + + for i in 0..n { + *cnt.entry(basket1[i]).or_insert(0) += 1; + *cnt.entry(basket2[i]).or_insert(0) -= 1; + } + + let mut mi = i32::MAX; + let mut nums = Vec::new(); + + for (x, v) in cnt { + if v % 2 != 0 { + return -1; + } + for _ in 0..(v.abs() / 2) { + nums.push(x); + } + mi = mi.min(x); + } + + nums.sort(); + + let m = nums.len(); + let mut ans = 0; + + for i in 0..(m / 2) { + ans += nums[i].min(mi * 2) as i64; + } + + ans + } +} +``` + diff --git a/solution/2500-2599/2561.Rearranging Fruits/Solution.cpp b/solution/2500-2599/2561.Rearranging Fruits/Solution.cpp index 4124066f0344f..b24e4bbffb1c3 100644 --- a/solution/2500-2599/2561.Rearranging Fruits/Solution.cpp +++ b/solution/2500-2599/2561.Rearranging Fruits/Solution.cpp @@ -18,7 +18,7 @@ class Solution { } mi = min(mi, x); } - sort(nums.begin(), nums.end()); + ranges::sort(nums); int m = nums.size(); long long ans = 0; for (int i = 0; i < m / 2; ++i) { diff --git a/solution/2500-2599/2561.Rearranging Fruits/Solution.rs b/solution/2500-2599/2561.Rearranging Fruits/Solution.rs new file mode 100644 index 0000000000000..0f654b992d1f9 --- /dev/null +++ b/solution/2500-2599/2561.Rearranging Fruits/Solution.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; + +impl Solution { + pub fn min_cost(basket1: Vec, basket2: Vec) -> i64 { + let n = basket1.len(); + let mut cnt: HashMap = HashMap::new(); + + for i in 0..n { + *cnt.entry(basket1[i]).or_insert(0) += 1; + *cnt.entry(basket2[i]).or_insert(0) -= 1; + } + + let mut mi = i32::MAX; + let mut nums = Vec::new(); + + for (x, v) in cnt { + if v % 2 != 0 { + return -1; + } + for _ in 0..(v.abs() / 2) { + nums.push(x); + } + mi = mi.min(x); + } + + nums.sort(); + + let m = nums.len(); + let mut ans = 0; + + for i in 0..(m / 2) { + ans += nums[i].min(mi * 2) as i64; + } + + ans + } +} diff --git a/solution/2500-2599/2561.Rearranging Fruits/Solution.ts b/solution/2500-2599/2561.Rearranging Fruits/Solution.ts new file mode 100644 index 0000000000000..01ade8f2b17c7 --- /dev/null +++ b/solution/2500-2599/2561.Rearranging Fruits/Solution.ts @@ -0,0 +1,27 @@ +function minCost(basket1: number[], basket2: number[]): number { + const n = basket1.length; + const cnt: Map = new Map(); + for (let i = 0; i < n; i++) { + cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1); + cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1); + } + let mi = Number.MAX_SAFE_INTEGER; + const nums: number[] = []; + for (const [x, v] of cnt.entries()) { + if (v % 2 !== 0) { + return -1; + } + for (let i = 0; i < Math.abs(v) / 2; i++) { + nums.push(x); + } + mi = Math.min(mi, x); + } + + nums.sort((a, b) => a - b); + const m = nums.length; + let ans = 0; + for (let i = 0; i < m / 2; i++) { + ans += Math.min(nums[i], mi * 2); + } + return ans; +} diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README.md b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README.md index 48864e35833c6..4b651475639ef 100644 --- a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README.md +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README.md @@ -148,32 +148,109 @@ tags: -### 方法一 +### 方法一:排序 + +我们直接按照题目要求的方式对数组进行排序即可。需要注意的是,分数是一个长整型数,因此在比较时需要使用长整型来避免溢出。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{threats}$ 的长度。 #### 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> sortThreats(vector>& threats) { + sort(threats.begin(), threats.end(), [](const vector& a, const vector& 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> { + 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 + } +} ``` diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README_EN.md b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README_EN.md index 75f7faac2b946..5da35fb5e52ef 100644 --- a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README_EN.md +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/README_EN.md @@ -146,32 +146,109 @@ tags: -### 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}$. #### 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> sortThreats(vector>& threats) { + sort(threats.begin(), threats.end(), [](const vector& a, const vector& 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> { + 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 + } +} ``` diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.cpp b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.cpp new file mode 100644 index 0000000000000..b0a253c8bfeb3 --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + vector> sortThreats(vector>& threats) { + sort(threats.begin(), threats.end(), [](const vector& a, const vector& 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; + } +}; diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.go b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.go new file mode 100644 index 0000000000000..62ddfd08e4fe6 --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.go @@ -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 +} diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.java b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.java new file mode 100644 index 0000000000000..c8f3d59ae52e4 --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.py b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.py new file mode 100644 index 0000000000000..54c981e1790b2 --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.py @@ -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 diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.rs b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.rs new file mode 100644 index 0000000000000..267d96688a381 --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn sort_threats(mut threats: Vec>) -> Vec> { + 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 + } +} diff --git a/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.ts b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.ts new file mode 100644 index 0000000000000..81cfa2474a25b --- /dev/null +++ b/solution/3600-3699/3631.Sort Threats by Severity and Exploitability/Solution.ts @@ -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; +}