LeetCode 2335. Minimum Amount of Time to Fill Cups

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:

  1. 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 abc steps.

    So the total steps are a a a steps.

  2. 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.

    1. 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

    2. if c+y is even

      define c+y = 2k because c+y is even.

      because y < c, so k < 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+ck=(bc)+c+k=b+k=b+2c+y=b+2c+ab=2a+b+c

    3. if c+y is even

      define 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+ck+1=2a+b+c+1=ceil(2a+b+c)

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);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值