896. Monotonic Array ¶ Time: Space: C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: bool isMonotonic(vector<int>& nums) { bool increasing = true; bool decreasing = true; for (int i = 1; i < nums.size(); ++i) { increasing &= nums[i] >= nums[i - 1]; decreasing &= nums[i] <= nums[i - 1]; } return increasing || decreasing; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public boolean isMonotonic(int[] nums) { boolean increasing = true; boolean decreasing = true; for (int i = 1; i < nums.length; ++i) { increasing &= nums[i] >= nums[i - 1]; decreasing &= nums[i] <= nums[i - 1]; } return increasing || decreasing; } } 1 2 3 4 5 6 7 8 9 10class Solution: def isMonotonic(self, nums: list[int]) -> bool: increasing = True decreasing = True for i in range(1, len(nums)): increasing &= nums[i - 1] <= nums[i] decreasing &= nums[i - 1] >= nums[i] return increasing or decreasing