Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd53c989

Browse files
authored
Solution for problem 1664 (fishercoder1534#139)
* Add solution for problem 1664* Add js file
1 parent07f97cb commitd53c989

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1664|[Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/)|[Javascript](./javascript/_1664.js)||Medium|Greedy|
1112
|1663|[Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1663.java)||Medium|Greedy|
1213
|1662|[Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1662.java)||Easy|String|
1314
|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js)||Medium|Greedy|

‎javascript/_1664.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Author: Phuong Lam
2+
3+
/**
4+
*@param {number[]} nums
5+
*@return {number}
6+
*/
7+
varwaysToMakeFair=function(nums){
8+
vartotal=0
9+
10+
varlo=0// left odd
11+
varle=0// left even
12+
varro=0// right odd
13+
varre=0// right even
14+
15+
// when ignore 1 element => right odd mean i % 2 === 0
16+
for(i=0;i<nums.length;i++){
17+
if(i%2===0)ro+=nums[i]
18+
elsere+=nums[i]
19+
}
20+
21+
// remove position i, recalculate left - right even-odd
22+
for(i=0;i<nums.length;i++){
23+
// recalculate left
24+
if(i>0){
25+
if(i%2===0)lo+=nums[i-1]
26+
elsele+=nums[i-1]
27+
}
28+
29+
// recalculate right
30+
if(i%2===0)ro-=nums[i]
31+
elsere-=nums[i]
32+
33+
// calc total even & odd
34+
if(lo+ro===le+re)total++
35+
}
36+
returntotal
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp