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; +}