mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
更新 problem 1005
This commit is contained in:
@ -1,13 +1,15 @@
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
import "sort"
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
func largestSumAfterKNegations(A []int, K int) int {
|
func largestSumAfterKNegations(A []int, K int) int {
|
||||||
sort.Ints(A)
|
sort.Ints(A)
|
||||||
minIdx := 0
|
minIdx := 0
|
||||||
for i := 0; i < K; i++ {
|
for i := 0; i < K; i++ {
|
||||||
A[minIdx] = -A[minIdx]
|
A[minIdx] = -A[minIdx]
|
||||||
if minIdx == len(A)-1 || A[minIdx+1] < A[minIdx] {
|
if A[minIdx+1] < A[minIdx] {
|
||||||
minIdx++
|
minIdx++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user