GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #525 - Contiguous Array - LeetCode JavaScript Solutions

Back to all solutions

#525 - Contiguous Array

Medium • View on LeetCode • View on GitHub
Array Hash Table Prefix Sum

Problem Description

Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.

Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxLength = function(nums) {
  const map = new Map([[0, -1]]);
  let result = 0;

  for (let i = 0, count = 0; i < nums.length; i++) {
    count += nums[i] === 1 ? 1 : -1;
    if (map.has(count)) {
      result = Math.max(result, i - map.get(count));
    } else {
      map.set(count, i);
    }
  }

  return result;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.