Similar Problems

Similar Problems not available

Minimum Cost To Set Cooking Time - Leetcode Solution

Companies:

LeetCode:  Minimum Cost To Set Cooking Time Leetcode Solution

Difficulty: Medium

Topics: math  

Problem Statement:

There are n dishes and for each dish, you know the time it needs to be cooked. You want to cook all the dishes, but you only have m burners available. Each burner can cook only one dish at a time and once you start cooking a dish, you must let it finish before starting another dish. It is okay to leave a burner unoccupied. However, if a dish is left uncooked for more than its cooking time, it will spoil and incur a penalty cost.

What is the minimum total penalty cost you can incur while cooking all the dishes?

Solution:

We can solve this problem using binary search and greedy approach. First, we need to define a function that takes a cooking time t and determines if it is possible to cook all the dishes within t time given m burners.

The function will iterate through all the dishes and assign each dish to an available burner. If all the burners are occupied, the function will wait until the first available burner becomes free. If a dish cannot be cooked within t time, then the function will return False. Otherwise, it will return True.

With this function, we can perform binary search over the range of possible values for t. The minimum possible value is the sum of all cooking times, and the maximum possible value is infinity since there is always a solution for t > max(cooking times).

In each iteration of binary search, we compute the mid value and call the function to check if it is possible to cook all the dishes within mid time given m burners. If it is possible, then we update the minimum cost and search for a smaller value of t. If it is not possible, then we search for a larger value of t.

Here is the Python code for the solution:

class Solution:
    def minCost(self, cookingTimes: List[int], m: int) -> int:
        # function that checks if it is possible to cook all dishes within time t
        def is_possible(t):
            burners = [0] * m
            for i in range(len(cookingTimes)):
                # find the earliest available burner to cook this dish
                j = burners.index(min(burners))
                if burners[j] > 0:
                    # wait until the burner becomes free
                    burners[j] += max(cookingTimes[i] - t, 0)
                else:
                    burners[j] += cookingTimes[i] - t
                if burners[j] > 0:
                    # this dish cannot be cooked within t time
                    return False
            # all dishes can be cooked within t time
            return True
        
        # binary search for the minimum cost
        l, r = sum(cookingTimes), float('inf')
        while l < r:
            mid = (l + r) // 2
            if is_possible(mid):
                r = mid
            else:
                l = mid + 1
        return l

Time Complexity: O(n log M), where n is the number of dishes and M is the maximum possible value of t. Space Complexity: O(m), where m is the number of burners.

Minimum Cost To Set Cooking Time Solution Code

1