Skip to content

Commit 7cb00fb

Browse files
committed
add 3736
1 parent e9c14ac commit 7cb00fb

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

javascript/LeetCode/Array/3736.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 3736. Minimum Moves to Equal Array Elements III
3+
*
4+
* 參數為數值陣列,在一次操作中可將任一元素+1
5+
* 回傳須移動幾次才能將所有元素都變得一樣
6+
*
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var minMoves = function(nums) {
11+
// 要先知道nums中最大值是多少,這樣就能知道其他元素跟最大值差多少
12+
let count = 0;
13+
let maxEle = Math.max(...nums);
14+
for(let i = 0;i < nums.length;++i) {
15+
count += Math.abs(maxEle - nums[i]);
16+
}
17+
return count;
18+
};
19+
let nums = [2,1,3];
20+
/*
21+
Output: 3
22+
Explanation:
23+
To make all elements equal:
24+
Increase nums[0] = 2 by 1 to make it 3.
25+
Increase nums[1] = 1 by 1 to make it 2.
26+
Increase nums[1] = 2 by 1 to make it 3.
27+
Now, all elements of nums are equal to 3. The minimum total moves is 3.
28+
*/
29+
console.log(minMoves(nums))

javascript/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ var minRemoval = function(nums, k) {
12501250
}
12511251
return nums.length - count;
12521252
};
1253-
let nums = [1,6,2,9], k = 3;
1253+
// let nums = [1,6,2,9], k = 3;
12541254
// 2
12551255
// Remove nums[0] = 1 and nums[3] = 9 to get nums = [6, 2].
12561256
// Now max = 6, min = 2 and max <= min * k as 6 <= 2 * 3. Thus, the answer is 2.
@@ -1276,4 +1276,6 @@ Explanation: You can either:
12761276
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
12771277
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
12781278
*/
1279-
// console.log(minimumDeletions(s));
1279+
// console.log(minimumDeletions(s));
1280+
1281+

0 commit comments

Comments
 (0)