Problem Statement in English


Approach


Step 1: Calculate if a k allows Koko to eat piles within h hours

  • Step 1.1: Calculate hours taken to eat piles[i] for a given k


Solution in Python


class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        db = defaultdict(int)

        for start, end in intervals:
            db[start]+=1
            db[end]-=1

        db[newInterval[0]]+=1
        db[newInterval[1]]-=1

        start = 0
        count = 0
        merged = []

        for location in sorted(db):
            if count == 0:
                start = location

            count += db[location]

            if count == 0:
                merged.append([start, location])

        return merged

Complexity

  • Time: $O()$

  • Space: $O()$


Mistakes I Made


And we are done.