Created
April 15, 2020 06:25
-
-
Save javi-aire/23c0511015dcfbd7a04bf019d4e023df to your computer and use it in GitHub Desktop.
Problem 13/30 of LeetCode 30-day challenge
This file contains hidden or 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
let findMaxLength = function(nums) { | |
const map = new Map(); | |
map.set(0, -1); | |
let count = 0, | |
max = 0; | |
for(let i = 0; i < nums.length; i++) { | |
if(nums[i] === 0){ | |
count--; | |
} else { | |
count++; | |
} | |
max = map.has(count) ? Math.max(max, i - map.get(count)) : max; | |
if (!map.has(count) || map.has(count) === -1) { | |
map.set(count, i); | |
} | |
} | |
return max; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment