Last active
September 9, 2018 11:17
-
-
Save frankchang0125/7eafacb8ff5ea9740bcc596bd6cd9638 to your computer and use it in GitHub Desktop.
665. Non-decreasing Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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