File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -240,6 +240,25 @@ class Solution:
240
240
```
241
241
242
242
Go:
243
+ ``` go
244
+ func canCompleteCircuit (gas []int , cost []int ) int {
245
+ curSum := 0
246
+ totalSum := 0
247
+ start := 0
248
+ for i := 0 ; i < len (gas); i++ {
249
+ curSum += gas[i] - cost[i]
250
+ totalSum += gas[i] - cost[i]
251
+ if curSum < 0 {
252
+ start = i+1
253
+ curSum = 0
254
+ }
255
+ }
256
+ if totalSum < 0 {
257
+ return -1
258
+ }
259
+ return start
260
+ }
261
+ ```
243
262
244
263
Javascript:
245
264
``` Javascript
Original file line number Diff line number Diff line change @@ -138,6 +138,30 @@ class Solution:
138
138
```
139
139
140
140
Go:
141
+ ``` Go
142
+ func largestSumAfterKNegations (nums []int , K int ) int {
143
+ sort.Slice (nums, func (i, j int ) bool {
144
+ return math.Abs (float64 (nums[i])) > math.Abs (float64 (nums[j]))
145
+ })
146
+
147
+ for i := 0 ; i < len (nums); i++ {
148
+ if K > 0 && nums[i] < 0 {
149
+ nums[i] = -nums[i]
150
+ K--
151
+ }
152
+ }
153
+
154
+ if K%2 == 1 {
155
+ nums[len (nums)-1 ] = -nums[len (nums)-1 ]
156
+ }
157
+
158
+ result := 0
159
+ for i := 0 ; i < len (nums); i++ {
160
+ result += nums[i]
161
+ }
162
+ return result
163
+ }
164
+ ```
141
165
142
166
143
167
Javascript:
You can’t perform that action at this time.
0 commit comments