Skip to content

Commit e3f766a

Browse files
solves monotonic array
1 parent 02e7084 commit e3f766a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | [![Java](assets/java.png)](src/FairCandySwap.java) |
243243
| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes) | [![Java](assets/java.png)](src/SurfaceAreaOf3DShapes.java) |
244244
| 893 | [Groups of Special Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings) | |
245-
| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array) | |
245+
| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array) | [![Java](assets/java.png)](src/MonotonicArray.java) |
246246
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree) | |
247247
| 905 | [Sort Array by Parity](https://leetcode.com/problems/sort-array-by-parity) | |
248248
| 908 | [Smallest Range I](https://leetcode.com/problems/smallest-range-i) | |

Diff for: src/MonotonicArray.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class MonotonicArray {
2+
public boolean isMonotonic(int[] nums) {
3+
return isMonotonicIncreasing(nums) || isMonotonicDecreasing(nums);
4+
}
5+
6+
private boolean isMonotonicIncreasing(int[] array) {
7+
for (int i = 1 ; i < array.length ; i++) {
8+
if (array[i] < array[i - 1]) return false;
9+
}
10+
return true;
11+
}
12+
13+
private boolean isMonotonicDecreasing(int[] array) {
14+
for (int i = 1 ; i < array.length ; i++) {
15+
if (array[i] > array[i - 1]) return false;
16+
}
17+
return true;
18+
}
19+
}

0 commit comments

Comments
 (0)