Skip to content

Commit 2bc0266

Browse files
committed
feat: update solutions to lc problems: No.2335,2336
* No.2335.Minimum Amount of Time to Fill Cups * No.2336.Smallest Number in Infinite Set
1 parent 3fe42a2 commit 2bc0266

File tree

7 files changed

+303
-18
lines changed

7 files changed

+303
-18
lines changed

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
**方法一:贪心 + 排序**
6363

64+
每次贪心地选择其中较大的两个数进行减一操作(最多减为 0),直至所有数变为 0。
65+
6466
<!-- tabs:start -->
6567

6668
### **Python3**
@@ -75,8 +77,7 @@ class Solution:
7577
amount.sort()
7678
ans += 1
7779
amount[2] -= 1
78-
if amount[1]:
79-
amount[1] -= 1
80+
amount[1] = max(0, amount[1] - 1)
8081
return ans
8182
```
8283

@@ -92,9 +93,7 @@ class Solution {
9293
Arrays.sort(amount);
9394
++ans;
9495
amount[2]--;
95-
if (amount[1] > 0) {
96-
amount[1]--;
97-
}
96+
amount[1] = Math.max(0, amount[1] - 1);
9897
}
9998
return ans;
10099
}
@@ -113,7 +112,7 @@ public:
113112
sort(amount.begin(), amount.end());
114113
++ans;
115114
amount[2]--;
116-
if (amount[1]) amount[1]--;
115+
amount[1] = max(0, amount[1] - 1);
117116
}
118117
return ans;
119118
}

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README_EN.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class Solution:
6868
amount.sort()
6969
ans += 1
7070
amount[2] -= 1
71-
if amount[1]:
72-
amount[1] -= 1
71+
amount[1] = max(0, amount[1] - 1)
7372
return ans
7473
```
7574

@@ -83,9 +82,7 @@ class Solution {
8382
Arrays.sort(amount);
8483
++ans;
8584
amount[2]--;
86-
if (amount[1] > 0) {
87-
amount[1]--;
88-
}
85+
amount[1] = Math.max(0, amount[1] - 1);
8986
}
9087
return ans;
9188
}
@@ -104,7 +101,7 @@ public:
104101
sort(amount.begin(), amount.end());
105102
++ans;
106103
amount[2]--;
107-
if (amount[1]) amount[1]--;
104+
amount[1] = max(0, amount[1] - 1);
108105
}
109106
return ans;
110107
}

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Solution {
77
sort(amount.begin(), amount.end());
88
++ans;
99
amount[2]--;
10-
if (amount[1]) amount[1]--;
10+
amount[1] = max(0, amount[1] - 1);
1111
}
1212
return ans;
1313
}

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ public int fillCups(int[] amount) {
55
Arrays.sort(amount);
66
++ans;
77
amount[2]--;
8-
if (amount[1] > 0) {
9-
amount[1]--;
10-
}
8+
amount[1] = Math.max(0, amount[1] - 1);
119
}
1210
return ans;
1311
}

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ def fillCups(self, amount: List[int]) -> int:
55
amount.sort()
66
ans += 1
77
amount[2] -= 1
8-
if amount[1]:
9-
amount[1] -= 1
8+
amount[1] = max(0, amount[1] - 1)
109
return ans

solution/2300-2399/2336.Smallest Number in Infinite Set/README.md

+147
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ smallestInfiniteSet.popSmallest(); // 返回 5 ,并将其从集合中移除。
5353

5454
**方法一:哈希表**
5555

56+
**方法二:哈希表 + 优先队列(小根堆)**
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
@@ -76,6 +78,31 @@ class SmallestInfiniteSet:
7678
self.black.discard(num)
7779

7880

81+
# Your SmallestInfiniteSet object will be instantiated and called as such:
82+
# obj = SmallestInfiniteSet()
83+
# param_1 = obj.popSmallest()
84+
# obj.addBack(num)
85+
```
86+
87+
```python
88+
class SmallestInfiniteSet:
89+
90+
def __init__(self):
91+
self.h = list(range(1, 1010))
92+
self.s = set(self.h)
93+
heapify(self.h)
94+
95+
def popSmallest(self) -> int:
96+
ans = heappop(self.h)
97+
self.s.discard(ans)
98+
return ans
99+
100+
def addBack(self, num: int) -> None:
101+
if num not in self.s:
102+
self.s.add(num)
103+
heappush(self.h, num)
104+
105+
79106
# Your SmallestInfiniteSet object will be instantiated and called as such:
80107
# obj = SmallestInfiniteSet()
81108
# param_1 = obj.popSmallest()
@@ -114,6 +141,40 @@ class SmallestInfiniteSet {
114141
*/
115142
```
116143

144+
```java
145+
class SmallestInfiniteSet {
146+
private PriorityQueue<Integer> pq = new PriorityQueue<>();
147+
private Set<Integer> s = new HashSet<>();
148+
149+
public SmallestInfiniteSet() {
150+
for (int i = 1; i < 1010; ++i) {
151+
pq.offer(i);
152+
s.add(i);
153+
}
154+
}
155+
156+
public int popSmallest() {
157+
int ans = pq.poll();
158+
s.remove(ans);
159+
return ans;
160+
}
161+
162+
public void addBack(int num) {
163+
if (!s.contains(num)) {
164+
s.add(num);
165+
pq.offer(num);
166+
}
167+
}
168+
}
169+
170+
/**
171+
* Your SmallestInfiniteSet object will be instantiated and called as such:
172+
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
173+
* int param_1 = obj.popSmallest();
174+
* obj.addBack(num);
175+
*/
176+
```
177+
117178
### **C++**
118179

119180
```cpp
@@ -145,6 +206,44 @@ public:
145206
*/
146207
```
147208

209+
```cpp
210+
class SmallestInfiniteSet {
211+
public:
212+
priority_queue<int, vector<int>, greater<int>> pq;
213+
unordered_set<int> s;
214+
215+
SmallestInfiniteSet() {
216+
for (int i = 1; i < 1010; ++i)
217+
{
218+
pq.push(i);
219+
s.insert(i);
220+
}
221+
}
222+
223+
int popSmallest() {
224+
int ans = pq.top();
225+
pq.pop();
226+
s.erase(ans);
227+
return ans;
228+
}
229+
230+
void addBack(int num) {
231+
if (!s.count(num))
232+
{
233+
s.insert(num);
234+
pq.push(num);
235+
}
236+
}
237+
};
238+
239+
/**
240+
* Your SmallestInfiniteSet object will be instantiated and called as such:
241+
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
242+
* int param_1 = obj->popSmallest();
243+
* obj->addBack(num);
244+
*/
245+
```
246+
148247
### **Go**
149248

150249
```go
@@ -177,6 +276,54 @@ func (this *SmallestInfiniteSet) AddBack(num int) {
177276
*/
178277
```
179278

279+
```go
280+
type SmallestInfiniteSet struct {
281+
h *hp
282+
s map[int]bool
283+
}
284+
285+
func Constructor() SmallestInfiniteSet {
286+
h := &hp{}
287+
s := map[int]bool{}
288+
for i := 1; i < 1010; i++ {
289+
s[i] = true
290+
h.push(i)
291+
}
292+
return SmallestInfiniteSet{h, s}
293+
}
294+
295+
func (this *SmallestInfiniteSet) PopSmallest() int {
296+
ans := this.h.pop()
297+
this.s[ans] = false
298+
return ans
299+
}
300+
301+
func (this *SmallestInfiniteSet) AddBack(num int) {
302+
if !this.s[num] {
303+
this.s[num] = true
304+
this.h.push(num)
305+
}
306+
}
307+
308+
type hp []int
309+
310+
func (h hp) Len() int { return len(h) }
311+
func (h hp) Less(i, j int) bool { return h[i] < h[j] }
312+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
313+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(int)) }
314+
func (h *hp) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }
315+
func (h *hp) push(v int) { heap.Push(h, v) }
316+
func (h *hp) pop() int { return heap.Pop(h).(int) }
317+
func (h *hp) top() int { a := *h; return a[0] }
318+
319+
/**
320+
* Your SmallestInfiniteSet object will be instantiated and called as such:
321+
* obj := Constructor();
322+
* param_1 := obj.PopSmallest();
323+
* obj.AddBack(num);
324+
*/
325+
```
326+
180327
### **TypeScript**
181328

182329
```ts

0 commit comments

Comments
 (0)