code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

485 Max Consecutive Ones

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Given a binary array, find the maximum number of consecutive 1s in this array.
 * 
 * Example 1:
 * Input: [1,1,0,1,1,1]
 * Output: 3
 * Explanation: The first two digits or the last three digits are consecutive 1s.
 *     The maximum number of consecutive 1s is 3.
 * Note:
 * 
 * The input array will only contain 0 and 1.
 * The length of input array is a positive integer and will not exceed 10,000
 */

function maxConsecutiveOnes(value) {
  let maxCount = 0
  let current = 0
  for (let index = 0; index < value.length; index++) {
    if(value[index] === 1){
       current += 1
    } else {
      maxCount = Math.max(maxCount, current)
      current = 0
    }
  }
  return Math.max(maxCount, current)
}

test('max Consecutive Ones', () => {
  expect(maxConsecutiveOnes([1,1,0,1,1,1])).toEqual(3)
});

Created 2020-04-18T20:57:26+00:00 · Edit