Go•monotonic-array/solution.go
package main
func isMonotonic(nums []int) bool {
nondecreasing, nonincreasing := true, true
for index := 1; index < len(nums); index++ {
if nums[index] < nums[index-1] {
nondecreasing = false
}
if nums[index] > nums[index-1] {
nonincreasing = false
}
}
return nondecreasing || nonincreasing
}
Python•monotonic-array/solution.py
class Solution:
def isMonotonic(self, nums: list[int]) -> bool:
nondecreasing = all(nums[index] >= nums[index - class="syntax-number">1] for index in range(class="syntax-number">1, len(nums)))
nonincreasing = all(nums[index] <= nums[index - class="syntax-number">1] for index in range(class="syntax-number">1, len(nums)))
return nondecreasing or nonincreasing
TypeScript•monotonic-array/solution.ts
function isMonotonic(nums: number[]): boolean {
let nondecreasing = true;
let nonincreasing = true;
for (let index = class="syntax-number">1; index < nums.length; index += class="syntax-number">1) { if (nums[index] < nums[index - class="syntax-number">1]) nondecreasing = false; if (nums[index] > nums[index - class="syntax-number">1]) nonincreasing = false; }
return nondecreasing || nonincreasing;
}