Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Monotonic Array Leetcode Problem with solution #37

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions LeetCode/Monotonic Array/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing.

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

Given an integer array nums, return true if the given array is monotonic, or false otherwise.



### Example 1:

Input: nums = [1,2,2,3]
Output: true

### Example 2:

Input: nums = [6,5,4,4]
Output: true

### Example 3:

Input: nums = [1,3,2]
Output: false


### Constraints:

1 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
Binary file added LeetCode/Monotonic Array/Python/Screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions LeetCode/Monotonic Array/Python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def isMonotonic(self, nums: list[int]) -> bool:
is_increasing = True # Indicates if the array is increasing.
is_decreasing = True # Indicates if the array is decreasing.

# Check if the array is either increasing or non-increasing.
for i in range(1, len(nums)):
# Check increasing condition.
if nums[i] < nums[i - 1]:
is_increasing = False

# Check decreasing condition.
elif nums[i] > nums[i - 1]:
is_decreasing = False

# If it is neither increasing nor decreasing then don't continue the loop.
if not is_increasing and not is_decreasing:
break

return is_increasing or is_decreasing # Return true if either condition is met