Skip to content

Commit ebcf450

Browse files
solves non decreasing array
1 parent 2a830bc commit ebcf450

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | [![Java](assets/java.png)](src/TwoSumIVInputIsABST.java) [![Python](assets/python.png)](python/two_sum_iv.py) |
172172
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | [![Java](assets/java.png)](src/RobotReturnToOrigin.java) [![Python](assets/python.png)](python/robot_return_to_origin.py) |
173173
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | [![Java](assets/java.png)](src/ImageSmoother.java) [![Python](assets/python.png)](python/image_smoother.py) |
174-
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | |
174+
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | [![Java](assets/java.png)](src/NonDecreasingArray.java) [![Python](assets/python.png)](python/non_decreasing_array.py) |
175175
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree) | |
176176
| 671 | [Second Minimum Node in Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree) | |
177177
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence) | |

Diff for: python/non_decreasing_array.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def checkPossibility(self, nums: List[int]) -> bool:
6+
handled_decreasing = False
7+
for index in range(len(nums) - 1):
8+
if nums[index] > nums[index + 1]:
9+
if handled_decreasing: return False
10+
handled_decreasing = True
11+
if index - 1 >= 0 and nums[index - 1] > nums[index + 1] and index + 2 < len(nums) and nums[index] > nums[index + 2]:
12+
return False
13+
return True

Diff for: src/NonDecreasingArray.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class NonDecreasingArray {
2+
public boolean checkPossibility(int[] array) {
3+
boolean handledDecreasing = false;
4+
for (int index = 0 ; index < array.length - 1 ; index++) {
5+
if (array[index] > array[index + 1]) {
6+
if (handledDecreasing) return false;
7+
handledDecreasing = true;
8+
if ((index - 1 >= 0 && array[index - 1] > array[index + 1]) && (index + 2 < array.length && array[index] > array[index + 2])) {
9+
return false;
10+
}
11+
}
12+
}
13+
return true;
14+
}
15+
}

0 commit comments

Comments
 (0)