↓Skip to main content
  1. LeetCode/

LeetCode 2335: Minimum Amount of Time to Fill Cups

·1 min· ·
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-11-22

  • Submissions: Accepted (first try)

Approach
#

At most two cups per second, of different types, gives two lower bounds: at least ceil(total / 2) seconds, and at least max(amount) since the largest type fills one cup per second. Greedily filling the two largest types each second (simulated with a max-heap, Greedy) achieves the larger bound exactly.

My Attempts
#

Passed on the first try with the formula above.

Solution
#

class Solution:
    def fillCups(self, amount: List[int]) -> int:
        return max(max(amount), (sum(amount) + 1) // 2)

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Consider both lower bounds and take the larger.

Interview Explanation
#

With two different cups per second, at least ceil(total / 2) seconds are needed, and the largest type needs at least its count; greedily filling the two largest types reaches the larger bound.

Related

LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 1464: Maximum Product of Two Elements in an Array
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 1464 note: all values are positive, so take the two largest a and b and return (a - 1)(b - 1)
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 1005: Maximize Sum Of Array After K Negations
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 1005 note: negate the smallest value each time; once the smallest is positive, only the parity of the remaining count matters
LeetCode 561: Array Partition
·1 min
LeetCode Easy Array Greedy Sorting Counting-Sort
LeetCode 561 note: sort and pair neighbors; each pair’s minimum is the element at an even index, so sum those
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once