Leetcode Note: Go - Monotonic Array
Monotonic Array - LeetCode
https://leetcode.com/problems/monotonic-array/
- Go 言語で取り組んだメモ
所感
- Monotonic Array か判定して bool を return する
- Monotonic Array: 要素が単調増加または単調減少する配列
回答
Monotonic Array - LeetCode
https://leetcode.com/problems/monotonic-array/solution/
[Golang] One Pass solution - LeetCode Discuss
https://leetcode.com/problems/monotonic-array/discuss/1640054/Golang-One-Pass-solution
func isMonotonic(nums []int) bool {
isIncreasing := true
isDecreasing := true
for i := 0; i < len(nums) - 1; i++ {
isIncreasing = isIncreasing && nums[i] <= nums[i + 1]
isDecreasing = isDecreasing && nums[i] >= nums[i + 1]
}
return isIncreasing || isDecreasing
}