内卷地狱

2335. The shortest total time to be filled with a cup One question daily

Edit Me

topic:

2335. The shortest total time to be filled with a cup.md

Thought:

  1. This question is very simple,但是我好像没用贪心的Thought。Look at the second test case and find it found,In fact, the minimum number of seconds is to avoid a certain number as much as possible0。 So keep sorting,Always operate the two largest numbers。This sending can also be used for DoriamountCase,But because of sorting,I don't know if it can be used for a large number。 Look atylbBig,和我一样但是Big细节处理得很好,Two fewer judgments than me,Then it seems that the classification discussion method below may be a solution to the difficult situation。
  2. mathematical method?Sort the number of drinks from small to large,Set the quantity x,y,z。Our goal is to match the different drinks as much as possible。 likex+y<=zx+y<=z,The answer isz。like反之,Then sett=(x+yz)t=(x+y-z),t是偶数The answer is t12+z \frac{t−1}{2} +zPlus one

Code:

class Solution:
    def fillCups(self, amount: List[int]) -> int:
        amount.sort()
        count = 0
        # Try to avoid returning0
        while amount[-1] > 0:
            if amount[-1] > 0 and amount[1] > 0:
                amount[-1] -= 1
                amount[1] -= 1
                count += 1
            if amount[-1] > 0 and amount[1] == 0:
                return count + amount[-1]
            amount.sort()
        return count
import "sort"

func fillCups(amount []int) int {
	ans := 0
	for amount[0] + amount[1] + amount[2] > 0 {
		sort.Ints(amount)
		ans ++
		amount[2] --
		if amount[1] > 0{
			amount[1] --
		}
	}
	return ans
}

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA