Merge branch 'youngyangyang04:master' into master

This commit is contained in:
藤露
2021-06-19 10:17:17 +08:00
committed by GitHub
2 changed files with 43 additions and 0 deletions

View File

@ -240,6 +240,25 @@ class Solution:
```
Go
```go
func canCompleteCircuit(gas []int, cost []int) int {
curSum := 0
totalSum := 0
start := 0
for i := 0; i < len(gas); i++ {
curSum += gas[i] - cost[i]
totalSum += gas[i] - cost[i]
if curSum < 0 {
start = i+1
curSum = 0
}
}
if totalSum < 0 {
return -1
}
return start
}
```
Javascript:
```Javascript

View File

@ -138,6 +138,30 @@ class Solution:
```
Go
```Go
func largestSumAfterKNegations(nums []int, K int) int {
sort.Slice(nums, func(i, j int) bool {
return math.Abs(float64(nums[i])) > math.Abs(float64(nums[j]))
})
for i := 0; i < len(nums); i++ {
if K > 0 && nums[i] < 0 {
nums[i] = -nums[i]
K--
}
}
if K%2 == 1 {
nums[len(nums)-1] = -nums[len(nums)-1]
}
result := 0
for i := 0; i < len(nums); i++ {
result += nums[i]
}
return result
}
```
Javascript: