File tree 3 files changed +109
-0
lines changed
solution/1700-1799/1705.Maximum Number of Eaten Apples
3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,44 @@ public:
139
139
};
140
140
```
141
141
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
+
142
180
### ** ...**
143
181
144
182
```
Original file line number Diff line number Diff line change @@ -129,6 +129,44 @@ public:
129
129
};
130
130
```
131
131
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
+
132
170
### ** ...**
133
171
134
172
```
Original file line number Diff line number Diff line change
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 }
You can’t perform that action at this time.
0 commit comments