Skip to content

Commit 058c3f6

Browse files
authored
Create 2449-minimum-number-of-operations-to-make-arrays-similar.js
1 parent 4266524 commit 058c3f6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[]} target
4+
* @return {number}
5+
*/
6+
const makeSimilar = function (nums, target) {
7+
const odd = [], even = []
8+
const todd = [], teven = []
9+
for(const e of nums) {
10+
if(e % 2 === 0) even.push(e)
11+
else odd.push(e)
12+
}
13+
for(const e of target) {
14+
if(e % 2 === 0) teven.push(e)
15+
else todd.push(e)
16+
}
17+
const sfn = (a, b) => a - b
18+
odd.sort(sfn)
19+
todd.sort(sfn)
20+
even.sort(sfn)
21+
teven.sort(sfn)
22+
let res = 0
23+
for(let i = 0, n = odd.length; i < n; i++) {
24+
res += Math.abs(odd[i] - todd[i]) / 2
25+
}
26+
for(let i = 0, n = even.length; i < n; i++) {
27+
res += Math.abs(even[i] - teven[i]) / 2
28+
}
29+
return res / 2
30+
}

0 commit comments

Comments
 (0)