See this article in my blog: https://dyingdown.github.io/2023/02/05/LeetCode-2335-Minimum-Amount-of-Time-to-Fill-Cups/
Problem Description
2335. Minimum Amount of Time to Fill Cups
Analysis
Denote the column value as a >= b >= c
From the above formula, we can get two situations:
-
a >= b + c
In this situation, we can easily get that, first, use b b b steps, then use c c c, steps, then use a − b − c a - b - c a−b−c steps.
So the total steps are a a a steps.
-
a < b + c
define b : = c + x b := c + x b:=c+x, a : = b + y = c + x + y a := b + y = c + x + y a:=b+y=c+x+y
So the stones are
[c+x+y, c+x, c]
In order to achieve min steps, we should distribute the a properly to b and c.
-
first, we use x steps to remove from the first two stones.
Now the stones are
[c+y, c, c]
since a >= b >= c, so
c+x+y < c+x + c => y < c
-
if
c+y
is evendefine
c+y = 2k
because c+y is even.because
y < c
, sok < c
so the stones are
[2k, c, c]
. So the steps are:- k steps for the first to stone,
[k, c-k, c]
- k steps for the first and third stone,
[0, c-k, c-k]
- c-k steps for the last two stones:
[0, 0, 0]
So in this case, the total steps are:
x + k + k + c − k = ( b − c ) + c + k = b + k = b + c + y 2 = b + c + a − b 2 = a + b + c 2 x + k + k + c - k \\ = (b - c) + c + k \\ = b + k = b + \frac{c + y}{2} = b + \frac{c+a-b}{2} \\ = \frac{a + b + c}{2} x+k+k+c−k=(b−c)+c+k=b+k=b+2c+y=b+2c+a−b=2a+b+c - k steps for the first to stone,
-
if
c+y
is evendefine
c+y = 2k + 1, k < c
so the stones are
[2k + 1, c, c]
and the steps are:- k steps for first two:
[k + 1, c - k, c - k]
- k steps for first and third stone :
[1, c-k, c-k]
- c - k steps for last two:
[1, 0, 0]
- 1 step for first stone
So the total steps are:
x + k + k + c − k + 1 = a + b + c 2 + 1 = c e i l ( a + b + c 2 ) x + k + k + c - k + 1 = \frac{a+b+c}{2} + 1 = ceil(\frac{a+b+c}{2}) x+k+k+c−k+1=2a+b+c+1=ceil(2a+b+c) - k steps for first two:
-
Code
class Solution {
public:
int fillCups(vector<int>& amount) {
int sum = 0;
int maxN = 0;
for(int i : amount) {
sum += i;
maxN = max(maxN, i);
}
return max((sum + 1) / 2, maxN);
}
};