code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

06 Fruit Baskets

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

const fruitBaskets = trees => {
  let left = 0
  let w = {}
  let max = 0
  for (let right = 0; right < trees.length; right++) {
    w[trees[right]] = w[trees[right]] + 1 || 1
    while (Object.keys(w).length > 2) {
      w[trees[left]] -= 1
      if (w[trees[left]] == 0) {
        delete w[trees[left]]
      }
      left += 1
    }
    max = Math.max(max, right - left + 1)
  }
  return max
}

console.log(fruitBaskets([1, 2, 3, 2, 2]))

Created 2022-02-04T04:03:39+00:00 · Edit