Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 1.09 KB

leetcode_904.md

File metadata and controls

41 lines (33 loc) · 1.09 KB

LeetCode Problems

904. Fruit Into Baskets

import kotlin.math.max
class Solution {
    fun totalFruit(fruits: IntArray): Int {
        var answer = 0
        var map = mutableMapOf<Int, Int>()
        var prev = 0
        // iterate
        for (curr in 0 until fruits.size) {
            val fruit = fruits[curr]
            map[fruit] = (map[fruit] ?: 0) + 1
            // if bucket is full, move window-prev cursor to reduce bucket to size 2
            while (map.size > 2) {
                val prevFruit = fruits[prev]
                val prevCount = map[prevFruit]!!
                // remove one-by-one from prev
                if (prevCount > 1) map[prevFruit] = prevCount - 1
                else map.remove(prevFruit)
                prev ++
            }
            // refresh answer with current bucket's full count
            answer = max(answer, map.values.sum())
        }
        return answer
    }
}