Skip to content

Commit 2485c22

Browse files
committed
chore: add daily leetcode post 2335. 装满杯子需要的最短总时长_translated
1 parent ab866bc commit 2485c22

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: 2335. The shortest total time to be filled with a cup One question daily
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - answer
7+
- - dp
8+
- - One question daily
9+
- - golang
10+
abbrlink: 4400daa1
11+
---
12+
# topic:
13+
14+
15+
[2335. The shortest total time to be filled with a cup.md](https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/description/)
16+
17+
# Thought:
18+
19+
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。
20+
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。
21+
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。
22+
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。
23+
like$x+y<=z$,The answer isz。like反之,Then set$t=(x+y-z)$,t是偶数The answer is
24+
$ \frac{t−1}{2} +z$Plus one
25+
26+
# Code:
27+
28+
```python
29+
class Solution:
30+
def fillCups(self, amount: List[int]) -> int:
31+
amount.sort()
32+
count = 0
33+
# Try to avoid returning0
34+
while amount[-1] > 0:
35+
if amount[-1] > 0 and amount[1] > 0:
36+
amount[-1] -= 1
37+
amount[1] -= 1
38+
count += 1
39+
if amount[-1] > 0 and amount[1] == 0:
40+
return count + amount[-1]
41+
amount.sort()
42+
return count
43+
```
44+
```go
45+
import "sort"
46+
47+
func fillCups(amount []int) int {
48+
ans := 0
49+
for amount[0] + amount[1] + amount[2] > 0 {
50+
sort.Ints(amount)
51+
ans ++
52+
amount[2] --
53+
if amount[1] > 0{
54+
amount[1] --
55+
}
56+
}
57+
return ans
58+
}
59+
```

0 commit comments

Comments
 (0)