Skip to content

Commit 89adcfc

Browse files
authored
feat: add solutions to lc problem: No.2561 (doocs#4610)
No.2561.Rearranging Fruits
1 parent e2c8c36 commit 89adcfc

File tree

5 files changed

+215
-3
lines changed

5 files changed

+215
-3
lines changed

solution/2500-2599/2561.Rearranging Fruits/README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public:
159159
}
160160
mi = min(mi, x);
161161
}
162-
sort(nums.begin(), nums.end());
162+
ranges::sort(nums);
163163
int m = nums.size();
164164
long long ans = 0;
165165
for (int i = 0; i < m / 2; ++i) {
@@ -206,6 +206,80 @@ func abs(x int) int {
206206
}
207207
```
208208

209+
#### TypeScript
210+
211+
```ts
212+
function minCost(basket1: number[], basket2: number[]): number {
213+
const n = basket1.length;
214+
const cnt: Map<number, number> = new Map();
215+
for (let i = 0; i < n; i++) {
216+
cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1);
217+
cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1);
218+
}
219+
let mi = Number.MAX_SAFE_INTEGER;
220+
const nums: number[] = [];
221+
for (const [x, v] of cnt.entries()) {
222+
if (v % 2 !== 0) {
223+
return -1;
224+
}
225+
for (let i = 0; i < Math.abs(v) / 2; i++) {
226+
nums.push(x);
227+
}
228+
mi = Math.min(mi, x);
229+
}
230+
231+
nums.sort((a, b) => a - b);
232+
const m = nums.length;
233+
let ans = 0;
234+
for (let i = 0; i < m / 2; i++) {
235+
ans += Math.min(nums[i], mi * 2);
236+
}
237+
return ans;
238+
}
239+
```
240+
241+
#### Rust
242+
243+
```rust
244+
use std::collections::HashMap;
245+
246+
impl Solution {
247+
pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {
248+
let n = basket1.len();
249+
let mut cnt: HashMap<i32, i32> = HashMap::new();
250+
251+
for i in 0..n {
252+
*cnt.entry(basket1[i]).or_insert(0) += 1;
253+
*cnt.entry(basket2[i]).or_insert(0) -= 1;
254+
}
255+
256+
let mut mi = i32::MAX;
257+
let mut nums = Vec::new();
258+
259+
for (x, v) in cnt {
260+
if v % 2 != 0 {
261+
return -1;
262+
}
263+
for _ in 0..(v.abs() / 2) {
264+
nums.push(x);
265+
}
266+
mi = mi.min(x);
267+
}
268+
269+
nums.sort();
270+
271+
let m = nums.len();
272+
let mut ans = 0;
273+
274+
for i in 0..(m / 2) {
275+
ans += nums[i].min(mi * 2) as i64;
276+
}
277+
278+
ans
279+
}
280+
}
281+
```
282+
209283
<!-- tabs:end -->
210284

211285
<!-- solution:end -->

solution/2500-2599/2561.Rearranging Fruits/README_EN.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public:
157157
}
158158
mi = min(mi, x);
159159
}
160-
sort(nums.begin(), nums.end());
160+
ranges::sort(nums);
161161
int m = nums.size();
162162
long long ans = 0;
163163
for (int i = 0; i < m / 2; ++i) {
@@ -204,6 +204,80 @@ func abs(x int) int {
204204
}
205205
```
206206

207+
#### TypeScript
208+
209+
```ts
210+
function minCost(basket1: number[], basket2: number[]): number {
211+
const n = basket1.length;
212+
const cnt: Map<number, number> = new Map();
213+
for (let i = 0; i < n; i++) {
214+
cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1);
215+
cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1);
216+
}
217+
let mi = Number.MAX_SAFE_INTEGER;
218+
const nums: number[] = [];
219+
for (const [x, v] of cnt.entries()) {
220+
if (v % 2 !== 0) {
221+
return -1;
222+
}
223+
for (let i = 0; i < Math.abs(v) / 2; i++) {
224+
nums.push(x);
225+
}
226+
mi = Math.min(mi, x);
227+
}
228+
229+
nums.sort((a, b) => a - b);
230+
const m = nums.length;
231+
let ans = 0;
232+
for (let i = 0; i < m / 2; i++) {
233+
ans += Math.min(nums[i], mi * 2);
234+
}
235+
return ans;
236+
}
237+
```
238+
239+
#### Rust
240+
241+
```rust
242+
use std::collections::HashMap;
243+
244+
impl Solution {
245+
pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {
246+
let n = basket1.len();
247+
let mut cnt: HashMap<i32, i32> = HashMap::new();
248+
249+
for i in 0..n {
250+
*cnt.entry(basket1[i]).or_insert(0) += 1;
251+
*cnt.entry(basket2[i]).or_insert(0) -= 1;
252+
}
253+
254+
let mut mi = i32::MAX;
255+
let mut nums = Vec::new();
256+
257+
for (x, v) in cnt {
258+
if v % 2 != 0 {
259+
return -1;
260+
}
261+
for _ in 0..(v.abs() / 2) {
262+
nums.push(x);
263+
}
264+
mi = mi.min(x);
265+
}
266+
267+
nums.sort();
268+
269+
let m = nums.len();
270+
let mut ans = 0;
271+
272+
for i in 0..(m / 2) {
273+
ans += nums[i].min(mi * 2) as i64;
274+
}
275+
276+
ans
277+
}
278+
}
279+
```
280+
207281
<!-- tabs:end -->
208282

209283
<!-- solution:end -->

solution/2500-2599/2561.Rearranging Fruits/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Solution {
1818
}
1919
mi = min(mi, x);
2020
}
21-
sort(nums.begin(), nums.end());
21+
ranges::sort(nums);
2222
int m = nums.size();
2323
long long ans = 0;
2424
for (int i = 0; i < m / 2; ++i) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {
5+
let n = basket1.len();
6+
let mut cnt: HashMap<i32, i32> = HashMap::new();
7+
8+
for i in 0..n {
9+
*cnt.entry(basket1[i]).or_insert(0) += 1;
10+
*cnt.entry(basket2[i]).or_insert(0) -= 1;
11+
}
12+
13+
let mut mi = i32::MAX;
14+
let mut nums = Vec::new();
15+
16+
for (x, v) in cnt {
17+
if v % 2 != 0 {
18+
return -1;
19+
}
20+
for _ in 0..(v.abs() / 2) {
21+
nums.push(x);
22+
}
23+
mi = mi.min(x);
24+
}
25+
26+
nums.sort();
27+
28+
let m = nums.len();
29+
let mut ans = 0;
30+
31+
for i in 0..(m / 2) {
32+
ans += nums[i].min(mi * 2) as i64;
33+
}
34+
35+
ans
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function minCost(basket1: number[], basket2: number[]): number {
2+
const n = basket1.length;
3+
const cnt: Map<number, number> = new Map();
4+
for (let i = 0; i < n; i++) {
5+
cnt.set(basket1[i], (cnt.get(basket1[i]) || 0) + 1);
6+
cnt.set(basket2[i], (cnt.get(basket2[i]) || 0) - 1);
7+
}
8+
let mi = Number.MAX_SAFE_INTEGER;
9+
const nums: number[] = [];
10+
for (const [x, v] of cnt.entries()) {
11+
if (v % 2 !== 0) {
12+
return -1;
13+
}
14+
for (let i = 0; i < Math.abs(v) / 2; i++) {
15+
nums.push(x);
16+
}
17+
mi = Math.min(mi, x);
18+
}
19+
20+
nums.sort((a, b) => a - b);
21+
const m = nums.length;
22+
let ans = 0;
23+
for (let i = 0; i < m / 2; i++) {
24+
ans += Math.min(nums[i], mi * 2);
25+
}
26+
return ans;
27+
}

0 commit comments

Comments
 (0)