Skip to content

Commit 185a355

Browse files
Create monotonic_array.py (#11025)
* Create monotonic_array.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update monotonic_array.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f336cca commit 185a355

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: data_structures/arrays/monotonic_array.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://leetcode.com/problems/monotonic-array/
2+
def is_monotonic(nums: list[int]) -> bool:
3+
"""
4+
Check if a list is monotonic.
5+
6+
>>> is_monotonic([1, 2, 2, 3])
7+
True
8+
>>> is_monotonic([6, 5, 4, 4])
9+
True
10+
>>> is_monotonic([1, 3, 2])
11+
False
12+
"""
13+
return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all(
14+
nums[i] >= nums[i + 1] for i in range(len(nums) - 1)
15+
)
16+
17+
18+
# Test the function with your examples
19+
if __name__ == "__main__":
20+
# Test the function with your examples
21+
print(is_monotonic([1, 2, 2, 3])) # Output: True
22+
print(is_monotonic([6, 5, 4, 4])) # Output: True
23+
print(is_monotonic([1, 3, 2])) # Output: False

0 commit comments

Comments
 (0)