MediumNo. 1760
Minimum Limit of Balls in a Bag — LeetCode 1760 Python Solution
- Pattern
- Monotonic Stack
- Topics
- Array, Binary Search
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an integer array nums where the ith bag contains nums[i] balls. You are also given an integer maxOperations.
Example
- Input
- nums = [9], maxOperations = 2
- Output
- 3
- Explanation
- - Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3].
Python solution
class Solution:
def minimumSize(self, nums: List[int], maxOperations: int) -> int:
def check(mx: int) -> bool:
return sum((x - 1) // mx for x in nums) <= maxOperations
return bisect_left(range(1, max(nums) + 1), True, key=check) + 1Complexity
- Time
- O(n × log M)where n and M are the length and the maximum value of the array nums, respectively
- Space · auxiliary
- O(1)
Pattern: Monotonic Stack
Answer "what is the next greater element" for every position in one pass. LeetCode 1760. Minimum Limit of Balls in a Bag is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.
The monotonic stack guide has the Python template for the pattern and the 225 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1751 | Maximum Number of Events That Can Be Attended II | Hard |
| 0004 | Median of Two Sorted Arrays | Hard |
| 0033 | Search in Rotated Sorted Array | Medium |
| 0034 | Find First and Last Position of Element in Sorted Array | Medium |
| 0035 | Search Insert Position | Easy |
| 0074 | Search a 2D Matrix | Medium |
Frequently asked questions
- How hard is LeetCode 1760. Minimum Limit of Balls in a Bag?
LeetCode 1760. Minimum Limit of Balls in a Bag is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1760. Minimum Limit of Balls in a Bag?
The Python solution on this page runs in O(n × log M), where n and M are the length and the maximum value of the array \textit{nums}, respectively.
- What is the space complexity of LeetCode 1760. Minimum Limit of Balls in a Bag?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1760. Minimum Limit of Balls in a Bag cover?
LeetCode 1760. Minimum Limit of Balls in a Bag is tagged Array and Binary Search on LeetCode.