Created
October 7, 2021 21:21
-
-
Save misterpoloy/7ea1421d3861a8ad3ae531b5f8ec260c to your computer and use it in GitHub Desktop.
Min number of swaps greedy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var minSwaps = function(s) { | |
let close = 0 | |
let maxClose = 0 | |
for (const letter of s.split('')) { | |
if (letter == "[") { | |
close -= 1 | |
} else { | |
close += 1 | |
} | |
maxClose = Math.max(maxClose, close) | |
} | |
return Math.floor((maxClose + 1) / 2) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original problem, Naiv solution
https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/