-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Is Monotonic | ||
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. | ||
""" | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def isMonotonic(self, nums: List[int]) -> bool: | ||
if nums[-1] - nums[0] < 0: | ||
nums.reverse() | ||
|
||
for i in range(len(nums) - 1): | ||
if not (nums[i] <= nums[i + 1]): | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
from is_monotonic import Solution | ||
|
||
|
||
class TestIsMonotonic(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.solution = Solution() | ||
|
||
def test_isMonotonic(self): | ||
self.assertTrue(self.solution.isMonotonic([1, 2, 2, 3])) | ||
|
||
self.assertTrue(self.solution.isMonotonic([6, 5, 4, 4])) | ||
|
||
self.assertFalse(self.solution.isMonotonic([1, 3, 2])) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |