Skip to content

Commit 45b88a6

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents ae38436 + a72bbac commit 45b88a6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

problems/0134.加油站.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ class Solution:
240240
```
241241

242242
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+
```
243262

244263
Javascript:
245264
```Javascript

problems/1005.K次取反后最大化的数组和.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ class Solution:
138138
```
139139

140140
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+
```
141165

142166

143167
Javascript:

0 commit comments

Comments
 (0)