Skip to content

Commit e6d96c8

Browse files
committed
Add solution #896
1 parent 073b1e9 commit e6d96c8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@
706706
893|[Groups of Special-Equivalent Strings](./0893-groups-of-special-equivalent-strings.js)|Medium|
707707
894|[All Possible Full Binary Trees](./0894-all-possible-full-binary-trees.js)|Medium|
708708
895|[Maximum Frequency Stack](./0895-maximum-frequency-stack.js)|Hard|
709+
896|[Monotonic Array](./0896-monotonic-array.js)|Easy|
709710
901|[Online Stock Span](./0901-online-stock-span.js)|Medium|
710711
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
711712
909|[Snakes and Ladders](./0909-snakes-and-ladders.js)|Medium|

solutions/0896-monotonic-array.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 896. Monotonic Array
3+
* https://leetcode.com/problems/monotonic-array/
4+
* Difficulty: Easy
5+
*
6+
* An array is monotonic if it is either monotone increasing or monotone decreasing.
7+
*
8+
* An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums
9+
* is monotone decreasing if for all i <= j, nums[i] >= nums[j].
10+
*
11+
* Given an integer array nums, return true if the given array is monotonic, or false otherwise.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {boolean}
17+
*/
18+
var isMonotonic = function(nums) {
19+
const isIncreasing = nums.every((num, i) => i === 0 || num >= nums[i - 1]);
20+
const isDecreasing = nums.every((num, i) => i === 0 || num <= nums[i - 1]);
21+
return isIncreasing || isDecreasing;
22+
};

0 commit comments

Comments
 (0)