Skip to content

Instantly share code, notes, and snippets.

@frankchang0125
Last active September 9, 2018 11:17
Show Gist options
  • Save frankchang0125/7eafacb8ff5ea9740bcc596bd6cd9638 to your computer and use it in GitHub Desktop.
Save frankchang0125/7eafacb8ff5ea9740bcc596bd6cd9638 to your computer and use it in GitHub Desktop.
665. Non-decreasing Array
func checkPossibility(nums []int) bool {
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
chance := true
max := MinInt
for i := 0; i < len(nums)-1; i++ {
if nums[i] > nums[i+1] {
if chance {
chance = false
if nums[i+1] > max {
nums[i] = nums[i+1]
} else if nums[i+1] < max {
if nums[i] > max {
nums[i+1] = nums[i]
} else {
nums[i+1] = max
}
}
} else {
return false
}
}
if nums[i] > max {
max = nums[i]
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment