class Solution:
def isMonotonic(self, nums):
increasing = decreasing = True
for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
decreasing = False
if nums[i] < nums[i-1]:
increasing = False
return increasing or decreasing
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
bool increasing = true, decreasing = true;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i-1]) decreasing = false;
if (nums[i] < nums[i-1]) increasing = false;
}
return increasing || decreasing;
}
};
class Solution {
public boolean isMonotonic(int[] nums) {
boolean increasing = true, decreasing = true;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) decreasing = false;
if (nums[i] < nums[i-1]) increasing = false;
}
return increasing || decreasing;
}
}
var isMonotonic = function(nums) {
let increasing = true, decreasing = true;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) decreasing = false;
if (nums[i] < nums[i-1]) increasing = false;
}
return increasing || decreasing;
};
Given an array of integers nums, determine if the array is monotonic. An array is monotonic if it is either entirely non-increasing or non-decreasing. In other words, for every index i (1 ≤ i < nums.length):
nums[i] >= nums[i-1] for all i (non-decreasing), ornums[i] <= nums[i-1] for all i (non-increasing).
Return true if the array is monotonic, and false otherwise.
Constraints:
nums.length ≤ 105nums[i] ≤ 105At first glance, the problem asks us to check if the numbers in the array never increase or never decrease as we move from left to right. A brute-force approach might involve checking every possible pair of elements, but that is unnecessary and inefficient.
Instead, we can observe that we only need to check the relationship between each consecutive pair of elements. If we ever see a pair where the order violates monotonicity in both directions, the array is not monotonic.
To optimize, we can keep track of two flags: one for non-decreasing and one for non-increasing. As we iterate, we update these flags, and if either remains true by the end, the array is monotonic.
increasing and decreasing, both set to true.
(nums[i-1], nums[i]), check if nums[i] is greater than nums[i-1]. If it is, set decreasing to false (since the sequence is not strictly non-increasing).nums[i] is less than nums[i-1], set increasing to false.increasing or decreasing is true, the array is monotonic.
increasing and decreasing are false, we can stop early (though the code above checks all elements for clarity).
This method is efficient, straightforward, and easy to implement.
Example Input: nums = [1, 2, 2, 3]
increasing = true, decreasing = true.decreasing = false. increasing remains true.decreasing remains false, increasing remains true.increasing = true, so return true.
Second Example: nums = [6, 5, 4, 4]
increasing = false.increasing remains false.decreasing = true, so return true.
Non-monotonic Example: nums = [1, 3, 2]
decreasing = false.increasing = false.false, so return false.This efficiency is critical for handling arrays of up to 105 elements.
To determine if an array is monotonic, we check for both non-decreasing and non-increasing patterns by iterating through consecutive pairs. By updating two flags, we can efficiently decide if the array meets the monotonicity condition in a single pass, using only constant extra space. This approach is elegant, easy to implement, and optimal for large datasets.