Skip to content

Commit 2fde25d

Browse files
add 665
1 parent 128524e commit 2fde25d

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|665|[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_665.java) | O(n) | O(n) | Easy |
2526
|664|[Strange Printer](https://leetcode.com/problems/strange-printer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_664.java) | O(n^3) | O(n^2) | Hard | DP
2627
|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | O(n) | O(n) | Medium | Tree
2728
|662|[Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_662.java) | O(n) | O(k) | Medium | BFS, DFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 665. Non-decreasing Array
5+
*
6+
* Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
7+
8+
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
9+
10+
Example 1:
11+
12+
Input: [4,2,3]
13+
Output: True
14+
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
15+
16+
Example 2:
17+
18+
Input: [4,2,1]
19+
Output: False
20+
Explanation: You can't get a non-decreasing array by modify at most one element.
21+
22+
Note: The n belongs to [1, 10,000].
23+
*/
24+
public class _665 {
25+
26+
public static class Solution1 {
27+
public boolean checkPossibility(int[] nums) {
28+
return modifyIMinusOne(nums) || modifyI(nums);
29+
}
30+
31+
private boolean modifyI(int[] nums) {
32+
int[] tmp = nums.clone();
33+
int len = tmp.length;
34+
int modifyTimes = 0;
35+
for (int i = 1; i < len; i++) {
36+
if (tmp[i] == tmp[i - 1]) {
37+
continue;
38+
}
39+
if (tmp[i] < tmp[i - 1]) {
40+
modifyTimes++;
41+
if (modifyTimes > 1) {
42+
return false;
43+
}
44+
tmp[i] = tmp[i - 1];
45+
}
46+
}
47+
for (int i = 1; i < len; i++) {
48+
if (tmp[i] == tmp[i - 1]) {
49+
continue;
50+
}
51+
if (tmp[i] < tmp[i - 1]) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
58+
private boolean modifyIMinusOne(int[] nums) {
59+
int[] tmp = nums.clone();
60+
int len = tmp.length;
61+
int modifyTimes = 0;
62+
for (int i = 1; i < len; i++) {
63+
if (tmp[i] == tmp[i - 1]) {
64+
continue;
65+
}
66+
if (tmp[i] < tmp[i - 1]) {
67+
modifyTimes++;
68+
if (modifyTimes > 1) {
69+
return false;
70+
}
71+
tmp[i - 1] = tmp[i];
72+
}
73+
}
74+
for (int i = 1; i < len; i++) {
75+
if (tmp[i] == tmp[i - 1]) {
76+
continue;
77+
}
78+
if (tmp[i] < tmp[i - 1]) {
79+
return false;
80+
}
81+
}
82+
return true;
83+
}
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._665;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _665Test {
10+
private static _665.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _665.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{4, 2, 3};
21+
assertEquals(true, solution1.checkPossibility(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{4, 2, 1};
27+
assertEquals(false, solution1.checkPossibility(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{3, 4, 2, 3};
33+
assertEquals(false, solution1.checkPossibility(nums));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
nums = new int[]{2, 3, 3, 2, 4};
39+
assertEquals(true, solution1.checkPossibility(nums));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
nums = new int[]{2, 3, 3, 2, 2, 4};
45+
assertEquals(false, solution1.checkPossibility(nums));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
nums = new int[]{2, 3, 3, 2, 2, 2, 4};
51+
assertEquals(false, solution1.checkPossibility(nums));
52+
}
53+
54+
@Test
55+
public void test7() {
56+
nums = new int[]{3, 3, 2, 2};
57+
assertEquals(false, solution1.checkPossibility(nums));
58+
}
59+
60+
@Test
61+
public void test8() {
62+
nums = new int[]{-1, 4, 2, 3};
63+
assertEquals(true, solution1.checkPossibility(nums));
64+
}
65+
66+
@Test
67+
public void test9() {
68+
nums = new int[]{1, 2, 4, 5, 3};
69+
assertEquals(true, solution1.checkPossibility(nums));
70+
}
71+
72+
@Test
73+
public void test10() {
74+
nums = new int[]{1, 2, 4, 5, 3, 6};
75+
assertEquals(true, solution1.checkPossibility(nums));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)