-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1551.使数组中所有元素相等的最小操作数.js
More file actions
32 lines (30 loc) · 964 Bytes
/
1551.使数组中所有元素相等的最小操作数.js
File metadata and controls
32 lines (30 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* @lc app=leetcode.cn id=1551 lang=javascript
*
* [1551] 使数组中所有元素相等的最小操作数
*/
// @lc code=start
/**
* @param {number} n
* @return {number}
*/
/**
* 1 [1] 0 (0)
* 2 [1,3] [2,2] 1 (1)
* 3 [1, 3, 5] [2,3,4] [3,3,3] 2 (2)
* 4 [1, 3, 5, 7] [2,3,5,6] [3,3,5,5] [4,3,5,4][4,4,4,4] 4 (3+1)
* 5 [1, 3, 5, 7, 9] [2,3,5,7,8][3,3,5,7,7][4,3,5,7,6][4,4,5,6,6][5,4,5,6,5][5,5,5,5,5] 6 (4+2)
* 6 [1,3,5,7,9,11] [2,3,5,7,9,10] [3,3,5,7,9,9] [4,3,5,7,9,8][4,4,5,7,8,8][5,4,5,7,8,7][5,5,5,7,7,7] [6,5,5,6,7,7]
* [6,6,5,6,6,7][6,6,6,6,6,6] 9 (5+3+1)
* 7 [1,3,5,7,9,11,13][2,3,5,7,9,11,12][3,3,5,7,9,11,11][4,3,5,7,9,11,10][4,4,5,7,9,10,10][5,4,5,7,9,10,9][5,5,5,7,9,9,9]
* [6,5,5,7,8,9,9][6,6,5,7,8,8,9][6,6,6,7,8,8,8] ... 12 (6+4+2)
*/
var minOperations = function (n) {
let res = 0;
let i = n % 2 ? 2 : 1;
for (; i < n; i += 2) {
res += i;
}
return res;
};
// @lc code=end