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

Solution for problem 1664#139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fishercoder1534 merged 2 commits intofishercoder1534:masterfromltp88:js-1664
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★

| # | Title | Solutions | Video | Difficulty | Tag
|-----|----------------|---------------|--------|-------------|-------------
|1664|[Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/)|[Javascript](./javascript/_1664.js) ||Medium|Greedy|
|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|
|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|
|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|
Expand Down
37 changes: 37 additions & 0 deletionsjavascript/_1664.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
// Author: Phuong Lam

/**
* @param {number[]} nums
* @return {number}
*/
var waysToMakeFair = function (nums) {
var total = 0

var lo = 0 // left odd
var le = 0 // left even
var ro = 0 // right odd
var re = 0 // right even

// when ignore 1 element => right odd mean i % 2 === 0
for (i = 0; i < nums.length; i++) {
if (i % 2 === 0) ro += nums[i]
else re += nums[i]
}

// remove position i, recalculate left - right even-odd
for (i = 0; i < nums.length; i++) {
// recalculate left
if (i > 0) {
if (i % 2 === 0) lo += nums[i - 1]
else le += nums[i - 1]
}

// recalculate right
if (i % 2 === 0) ro -= nums[i]
else re -= nums[i]

// calc total even & odd
if (lo + ro === le + re) total++
}
return total
}

[8]ページ先頭

©2009-2025 Movatter.jp