Skip to content

[pull] main from doocs:main #114

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 2 commits 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
76 changes: 75 additions & 1 deletion solution/2500-2599/2561.Rearranging Fruits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<number, number> = 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<i32>, basket2: Vec<i32>) -> i64 {
let n = basket1.len();
let mut cnt: HashMap<i32, i32> = 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
76 changes: 75 additions & 1 deletion solution/2500-2599/2561.Rearranging Fruits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<number, number> = 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<i32>, basket2: Vec<i32>) -> i64 {
let n = basket1.len();
let mut cnt: HashMap<i32, i32> = 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/2500-2599/2561.Rearranging Fruits/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions solution/2500-2599/2561.Rearranging Fruits/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::collections::HashMap;

impl Solution {
pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {
let n = basket1.len();
let mut cnt: HashMap<i32, i32> = 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
}
}
27 changes: 27 additions & 0 deletions solution/2500-2599/2561.Rearranging Fruits/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function minCost(basket1: number[], basket2: number[]): number {
const n = basket1.length;
const cnt: Map<number, number> = 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;
}
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
Loading
Loading