Skip to content

Commit a23747d

Browse files
committed
feat: add golang solution to lc problem: No.1705
No.1705.Maximum Number of Eaten Apples
1 parent 0fb490f commit a23747d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

Diff for: solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,44 @@ public:
139139
};
140140
```
141141
142+
### **Go**
143+
144+
```go
145+
func eatenApples(apples []int, days []int) int {
146+
var h hp
147+
ans, n := 0, len(apples)
148+
for i := 0; i < n || len(h) > 0; i++ {
149+
if i < n && apples[i] > 0 {
150+
heap.Push(&h, pair{i + days[i] - 1, apples[i]})
151+
}
152+
for len(h) > 0 && h[0].first < i {
153+
heap.Pop(&h)
154+
}
155+
if len(h) > 0 {
156+
h[0].second--
157+
if h[0].first == i || h[0].second == 0 {
158+
heap.Pop(&h)
159+
}
160+
ans++
161+
}
162+
}
163+
return ans
164+
}
165+
166+
type pair struct {
167+
first int
168+
second int
169+
}
170+
171+
type hp []pair
172+
173+
func (a hp) Len() int { return len(a) }
174+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
175+
func (a hp) Less(i, j int) bool { return a[i].first < a[j].first }
176+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
177+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
178+
```
179+
142180
### **...**
143181

144182
```

Diff for: solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,44 @@ public:
129129
};
130130
```
131131
132+
### **Go**
133+
134+
```go
135+
func eatenApples(apples []int, days []int) int {
136+
var h hp
137+
ans, n := 0, len(apples)
138+
for i := 0; i < n || len(h) > 0; i++ {
139+
if i < n && apples[i] > 0 {
140+
heap.Push(&h, pair{i + days[i] - 1, apples[i]})
141+
}
142+
for len(h) > 0 && h[0].first < i {
143+
heap.Pop(&h)
144+
}
145+
if len(h) > 0 {
146+
h[0].second--
147+
if h[0].first == i || h[0].second == 0 {
148+
heap.Pop(&h)
149+
}
150+
ans++
151+
}
152+
}
153+
return ans
154+
}
155+
156+
type pair struct {
157+
first int
158+
second int
159+
}
160+
161+
type hp []pair
162+
163+
func (a hp) Len() int { return len(a) }
164+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
165+
func (a hp) Less(i, j int) bool { return a[i].first < a[j].first }
166+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
167+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
168+
```
169+
132170
### **...**
133171

134172
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func eatenApples(apples []int, days []int) int {
2+
var h hp
3+
ans, n := 0, len(apples)
4+
for i := 0; i < n || len(h) > 0; i++ {
5+
if i < n && apples[i] > 0 {
6+
heap.Push(&h, pair{i + days[i] - 1, apples[i]})
7+
}
8+
for len(h) > 0 && h[0].first < i {
9+
heap.Pop(&h)
10+
}
11+
if len(h) > 0 {
12+
h[0].second--
13+
if h[0].first == i || h[0].second == 0 {
14+
heap.Pop(&h)
15+
}
16+
ans++
17+
}
18+
}
19+
return ans
20+
}
21+
22+
type pair struct {
23+
first int
24+
second int
25+
}
26+
27+
type hp []pair
28+
29+
func (a hp) Len() int { return len(a) }
30+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
31+
func (a hp) Less(i, j int) bool { return a[i].first < a[j].first }
32+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
33+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }

0 commit comments

Comments
 (0)