Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bc837e

Browse files
committed
665_Non-decreasing_Array
1 parent 08e37c4 commit 3bc837e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Also, there are open source implementations for basic data structs and algorithm
166166
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
167167
| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/628_Maximum_Product_of_Three_Numbers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/628_Maximum_Product_of_Three_Numbers.java) | Actually, we should only care about min1, min2 and max1-max3, to find these five elements, we can use 1. Brute force, O(n^3) and O(1)<br>2. Sort, O(nlogn) and O(1)<br>3. Single scan with 5 elements keep, O(n) and O(1) |
168168
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
169+
| 665 | [Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/665_Non-decreasing_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/665_Non-decreasing_Array.java) | 1. Find the broken index, then check this point, O(n) and O(1)<br>2. Replace broken point with correct value, then check if there are more than 1 broken point, O(n) and O(1) |
169170
| 671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/671_Second_Minimum_Node_In_a_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/671_Second_Minimum_Node_In_a_Binary_Tree.java) | Note that min value is root: 1. Get all values then find result, O(n) and O(n)<br>2. BFS or DFS traverse the tree, then find the reslut, O(n) and O(n)|
170171
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/674_Longest_Continuous_Increasing_Subsequence.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/674_Longest_Continuous_Increasing_Subsequence.java) | Scan nums once, check nums[i] < nums[i+1], if not reset count, O(n) and O(1) |
171172
| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. |

java/665_Non-decreasing_Array.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
/* public boolean checkPossibility(int[] nums) {
3+
int pos = -1;
4+
for (int i = 0; i < nums.length - 1; i++) {
5+
if (nums[i] > nums[i + 1]) {
6+
// More than two broken points
7+
if (pos != -1) return false;
8+
pos = i;
9+
}
10+
}
11+
if (pos == -1 || pos == 0 || pos == nums.length - 2) return true;
12+
// Remove pos or pos + 1
13+
return (nums[pos - 1] <= nums[pos + 1] || nums[pos] <= nums[pos + 2]);
14+
} */
15+
16+
public boolean checkPossibility(int[] nums) {
17+
int brokenPoint = 0;
18+
for (int i = 0; i < nums.length - 1; i++) {
19+
if (nums[i] > nums[i + 1]) {
20+
brokenPoint++;
21+
if (brokenPoint >= 2) return false;
22+
// Remove i or remove i + 1
23+
if (i - 1 < 0 || nums[i - 1] <= nums[i + 1]) nums[i] = nums[i + 1];
24+
else nums[i + 1] = nums[i];
25+
}
26+
}
27+
return true;
28+
}
29+
}

python/665_Non-decreasing_Array.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
# def checkPossibility(self, nums):
3+
# """
4+
# :type nums: List[int]
5+
# :rtype: bool
6+
# """
7+
# pos = None
8+
# # Check if there are more than 2 broken point
9+
# # record first index
10+
# for i in range(len(nums) - 1):
11+
# if nums[i] > nums[i + 1]:
12+
# if pos is not None:
13+
# return False
14+
# pos = i
15+
# if pos is None or pos == 0 or pos == len(nums) - 2:
16+
# return True
17+
# # Remove pos or remove pos + 1
18+
# return (nums[pos - 1] <= nums[pos + 1] or nums[pos] <= nums[pos + 2])
19+
20+
def checkPossibility(self, nums):
21+
"""
22+
:type nums: List[int]
23+
:rtype: bool
24+
"""
25+
# https://leetcode.com/problems/non-decreasing-array/discuss/106826/JavaC%2B%2B-Simple-greedy-like-solution-with-explanation
26+
broken_num = 0
27+
for i in range(len(nums) - 1):
28+
if (nums[i] > nums[i + 1]):
29+
broken_num += 1
30+
if broken_num >= 2:
31+
return False
32+
if (i - 1 < 0 or nums[i - 1] <= nums[i + 1]):
33+
# Remove i
34+
nums[i] = nums[i + 1]
35+
else:
36+
# Remove i + 1
37+
nums[i + 1] = nums[i]
38+
return True

0 commit comments

Comments
 (0)