mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-02-27 11:58:39 +08:00
routine
This commit is contained in:
38
26/01/go/3507.go
Normal file
38
26/01/go/3507.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package A
|
||||
|
||||
import "math"
|
||||
|
||||
func isIncrease(nums []int) bool {
|
||||
for i := range nums {
|
||||
if i == len(nums)-1 {
|
||||
break
|
||||
}
|
||||
if nums[i] > nums[i+1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func minimumPairRemoval(nums []int) int {
|
||||
ans := 0
|
||||
index := 0
|
||||
temp := 0
|
||||
for !isIncrease(nums) {
|
||||
minX := math.MaxInt
|
||||
for i := range nums {
|
||||
if i == len(nums)-1 {
|
||||
break
|
||||
}
|
||||
temp = nums[i] + nums[i+1]
|
||||
if minX > temp {
|
||||
minX = temp
|
||||
index = i
|
||||
}
|
||||
}
|
||||
nums[index] = minX
|
||||
nums = append(nums[:index+1], nums[index+2:]...)
|
||||
ans++
|
||||
}
|
||||
return ans
|
||||
}
|
||||
Reference in New Issue
Block a user