left
to 1 and right
to the maximum number of balls in any bag.left
is less than right
:
a. Calculate mid
as the average of left
and right
.
b. Initialize operations
to 0.
c. Iterate over each bag in nums
:
i. Calculate the number of operations needed to reduce the current bag to a size of mid
or less.
ii. Add this number to operations
.
d. If operations
is less than or equal to maxOperations
, update right
to mid
.
e. Otherwise, update left
to mid + 1
.left
as the minimum possible penalty.