Skip to content

Commit 74e8519

Browse files
committed
1664. 生成平衡数组的方案数
1 parent 71d5b72 commit 74e8519

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
|1002|[查找共用字符](https://leetcode.cn/problems/find-common-characters/)|[JavaScript](./algorithms/find-common-characters.js)|Easy|
191191
|1038|[从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/)|[JavaScript](./algorithms/binary-search-tree-to-greater-sum-tree.js)|Medium|
192192
|1047|[删除字符串中的所有相邻重复项](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)|[JavaScript](./algorithms/remove-all-adjacent-duplicates-in-string.js)|Easy|
193+
|1664|[生成平衡数组的方案数](https://leetcode.cn/problems/ways-to-make-a-fair-array/)|[JavaScript](./algorithms/ways-to-make-a-fair-array.js)|Medium|
193194
|1780|[判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/)|[JavaScript](./algorithms/check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
194195
|1807|[替换字符串中的括号内容](https://leetcode.cn/problems/evaluate-the-bracket-pairs-of-a-string/)|[JavaScript](./algorithms/evaluate-the-bracket-pairs-of-a-string.js)|Medium|
195196
|1813|[句子相似性 III](https://leetcode.cn/problems/sentence-similarity-iii/)|[JavaScript](./algorithms/sentence-similarity-iii.js)|Medium|
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1664. 生成平衡数组的方案数
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var waysToMakeFair = function (nums) {
7+
/**
8+
* [2, 1, 6, 4]
9+
* even: 8; odd: 5
10+
* | index | even | odd | 8 | 5 |
11+
* | 0 | 1+4=5 | 6 | 6 | 5 |
12+
* | 1 | 2+4+6 | 6 | 6 | 4 |
13+
* | 2 | 2+4=6 | 1 | 0 | 4 |
14+
* | 3 | 2+6=8 | 1 | 0 | 1 |
15+
* 总体思路: 前缀后缀`奇偶`和
16+
*/
17+
let odd1 = 0, even1 = 0; // 后缀和
18+
for (let i = 0; i < nums.length; i++) {
19+
if (i % 2 === 0) {
20+
even1 += nums[i];
21+
} else {
22+
odd1 += nums[i];
23+
}
24+
}
25+
let odd2 = 0, even2 = 0; // 前缀和
26+
let count = 0;
27+
for (let i = 0; i < nums.length; i++) {
28+
if (i % 2 === 0) {
29+
even1 -= nums[i];
30+
} else {
31+
odd1 -= nums[i];
32+
}
33+
if (odd1 + even2 === even1 + odd2) {
34+
count++;
35+
}
36+
if (i % 2 === 0) {
37+
even2 += nums[i];
38+
} else {
39+
odd2 += nums[i];
40+
}
41+
}
42+
43+
return count;
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @lc app=leetcode.cn id=1664 lang=javascript
3+
*
4+
* [1664] 生成平衡数组的方案数
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var waysToMakeFair = function(nums) {
13+
/**
14+
* [2, 1, 6, 4]
15+
* even: 8; odd: 5
16+
* | index | even | odd | 8 | 5 |
17+
* | 0 | 1+4=5 | 6 | 6 | 5 |
18+
* | 1 | 2+4+6 | 6 | 6 | 4 |
19+
* | 2 | 2+4=6 | 1 | 0 | 4 |
20+
* | 3 | 2+6=8 | 1 | 0 | 1 |
21+
* 总体思路: 前缀后缀`奇偶`和
22+
*/
23+
let odd1 = 0, even1 = 0; // 后缀和
24+
for (let i = 0; i < nums.length; i++) {
25+
if (i % 2 === 0) {
26+
even1 += nums[i];
27+
} else {
28+
odd1 += nums[i];
29+
}
30+
}
31+
let odd2 = 0, even2 = 0; // 前缀和
32+
let count = 0;
33+
for (let i = 0; i < nums.length; i++) {
34+
if (i % 2 === 0) {
35+
even1 -= nums[i];
36+
} else {
37+
odd1 -= nums[i];
38+
}
39+
if (odd1 + even2 === even1 + odd2) {
40+
count++;
41+
}
42+
if (i % 2 === 0) {
43+
even2 += nums[i];
44+
} else {
45+
odd2 += nums[i];
46+
}
47+
}
48+
49+
return count;
50+
};
51+
// @lc code=end
52+

0 commit comments

Comments
 (0)