Skip to content

[pull] main from doocs:main #29

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 1, 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;
}
Loading