Skip to content

Commit 10a1258

Browse files
committedMar 22, 2023
feat: add solutions to lc problem: No.0665
No.0665.Non-decreasing Array
1 parent 914bccc commit 10a1258

File tree

6 files changed

+275
-23
lines changed

6 files changed

+275
-23
lines changed
 

Diff for: ‎solution/0600-0699/0665.Non-decreasing Array/README.md

+102-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,123 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:两次遍历**
47+
48+
在最多改变一个元素的情况下,若要将数组变成非递减数列,那么数组最多只能有一个位置,其左右两侧的元素不满足非递减数列的要求。也即数组中只会存在一个位置 $i$,使得 $nums[i] \gt nums[i+1]$。
49+
50+
因此,我们可以从左到右遍历数组,找到第一个不满足非递减数列要求的位置 $i$,然后将 $nums[i]$ 修改为 $nums[i+1]$ 或者将 $nums[i+1]$ 修改为 $nums[i]$,再判断修改后的数组是否满足非递减数列的要求。如果满足,则返回 `true`,否则返回 `false`
51+
52+
遍历结束后,如果没有找到不满足非递减数列要求的位置,说明数组本身就是非递减数列,返回 `true`
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**
4959

5060
<!-- 这里可写当前语言的特殊实现逻辑 -->
5161

5262
```python
53-
63+
class Solution:
64+
def checkPossibility(self, nums: List[int]) -> bool:
65+
def is_sorted(nums: List[int]) -> bool:
66+
return all(a <= b for a, b in pairwise(nums))
67+
68+
n = len(nums)
69+
for i in range(n - 1):
70+
a, b = nums[i], nums[i + 1]
71+
if a > b:
72+
nums[i] = b
73+
if is_sorted(nums):
74+
return True
75+
nums[i] = nums[i + 1] = a
76+
return is_sorted(nums)
77+
return True
5478
```
5579

5680
### **Java**
5781

5882
<!-- 这里可写当前语言的特殊实现逻辑 -->
5983

6084
```java
85+
class Solution {
86+
public boolean checkPossibility(int[] nums) {
87+
for (int i = 0; i < nums.length - 1; ++i) {
88+
int a = nums[i], b = nums[i + 1];
89+
if (a > b) {
90+
nums[i] = b;
91+
if (isSorted(nums)) {
92+
return true;
93+
}
94+
nums[i] = a;
95+
nums[i + 1] = a;
96+
return isSorted(nums);
97+
}
98+
}
99+
return true;
100+
}
101+
102+
private boolean isSorted(int[] nums) {
103+
for (int i = 0; i < nums.length - 1; ++i) {
104+
if (nums[i] > nums[i + 1]) {
105+
return false;
106+
}
107+
}
108+
return true;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
bool checkPossibility(vector<int>& nums) {
119+
int n = nums.size();
120+
for (int i = 0; i < n - 1; ++i) {
121+
int a = nums[i], b = nums[i + 1];
122+
if (a > b) {
123+
nums[i] = b;
124+
if (is_sorted(nums.begin(), nums.end())) {
125+
return true;
126+
}
127+
nums[i] = a;
128+
nums[i + 1] = a;
129+
return is_sorted(nums.begin(), nums.end());
130+
}
131+
}
132+
return true;
133+
}
134+
};
135+
```
61136
137+
### **Go**
138+
139+
```go
140+
func checkPossibility(nums []int) bool {
141+
isSorted := func(nums []int) bool {
142+
for i, b := range nums[1:] {
143+
if nums[i] > b {
144+
return false
145+
}
146+
}
147+
return true
148+
}
149+
for i := 0; i < len(nums)-1; i++ {
150+
a, b := nums[i], nums[i+1]
151+
if a > b {
152+
nums[i] = b
153+
if isSorted(nums) {
154+
return true
155+
}
156+
nums[i] = a
157+
nums[i+1] = a
158+
return isSorted(nums)
159+
}
160+
}
161+
return true
162+
}
62163
```
63164

64165
### **...**

Diff for: ‎solution/0600-0699/0665.Non-decreasing Array/README_EN.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,104 @@
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def checkPossibility(self, nums: List[int]) -> bool:
46+
def is_sorted(nums: List[int]) -> bool:
47+
return all(a <= b for a, b in pairwise(nums))
48+
49+
n = len(nums)
50+
for i in range(n - 1):
51+
a, b = nums[i], nums[i + 1]
52+
if a > b:
53+
nums[i] = b
54+
if is_sorted(nums):
55+
return True
56+
nums[i] = nums[i + 1] = a
57+
return is_sorted(nums)
58+
return True
4559
```
4660

4761
### **Java**
4862

4963
```java
64+
class Solution {
65+
public boolean checkPossibility(int[] nums) {
66+
for (int i = 0; i < nums.length - 1; ++i) {
67+
int a = nums[i], b = nums[i + 1];
68+
if (a > b) {
69+
nums[i] = b;
70+
if (isSorted(nums)) {
71+
return true;
72+
}
73+
nums[i] = a;
74+
nums[i + 1] = a;
75+
return isSorted(nums);
76+
}
77+
}
78+
return true;
79+
}
80+
81+
private boolean isSorted(int[] nums) {
82+
for (int i = 0; i < nums.length - 1; ++i) {
83+
if (nums[i] > nums[i + 1]) {
84+
return false;
85+
}
86+
}
87+
return true;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool checkPossibility(vector<int>& nums) {
98+
int n = nums.size();
99+
for (int i = 0; i < n - 1; ++i) {
100+
int a = nums[i], b = nums[i + 1];
101+
if (a > b) {
102+
nums[i] = b;
103+
if (is_sorted(nums.begin(), nums.end())) {
104+
return true;
105+
}
106+
nums[i] = a;
107+
nums[i + 1] = a;
108+
return is_sorted(nums.begin(), nums.end());
109+
}
110+
}
111+
return true;
112+
}
113+
};
114+
```
50115
116+
### **Go**
117+
118+
```go
119+
func checkPossibility(nums []int) bool {
120+
isSorted := func(nums []int) bool {
121+
for i, b := range nums[1:] {
122+
if nums[i] > b {
123+
return false
124+
}
125+
}
126+
return true
127+
}
128+
for i := 0; i < len(nums)-1; i++ {
129+
a, b := nums[i], nums[i+1]
130+
if a > b {
131+
nums[i] = b
132+
if isSorted(nums) {
133+
return true
134+
}
135+
nums[i] = a
136+
nums[i+1] = a
137+
return isSorted(nums)
138+
}
139+
}
140+
return true
141+
}
51142
```
52143

53144
### **...**
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool checkPossibility(vector<int>& nums) {
4+
int n = nums.size();
5+
for (int i = 0; i < n - 1; ++i) {
6+
int a = nums[i], b = nums[i + 1];
7+
if (a > b) {
8+
nums[i] = b;
9+
if (is_sorted(nums.begin(), nums.end())) {
10+
return true;
11+
}
12+
nums[i] = a;
13+
nums[i + 1] = a;
14+
return is_sorted(nums.begin(), nums.end());
15+
}
16+
}
17+
return true;
18+
}
19+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func checkPossibility(nums []int) bool {
2+
isSorted := func(nums []int) bool {
3+
for i, b := range nums[1:] {
4+
if nums[i] > b {
5+
return false
6+
}
7+
}
8+
return true
9+
}
10+
for i := 0; i < len(nums)-1; i++ {
11+
a, b := nums[i], nums[i+1]
12+
if a > b {
13+
nums[i] = b
14+
if isSorted(nums) {
15+
return true
16+
}
17+
nums[i] = a
18+
nums[i+1] = a
19+
return isSorted(nums)
20+
}
21+
}
22+
return true
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean checkPossibility(int[] nums) {
3+
for (int i = 0; i < nums.length - 1; ++i) {
4+
int a = nums[i], b = nums[i + 1];
5+
if (a > b) {
6+
nums[i] = b;
7+
if (isSorted(nums)) {
8+
return true;
9+
}
10+
nums[i] = a;
11+
nums[i + 1] = a;
12+
return isSorted(nums);
13+
}
14+
}
15+
return true;
16+
}
17+
18+
private boolean isSorted(int[] nums) {
19+
for (int i = 0; i < nums.length - 1; ++i) {
20+
if (nums[i] > nums[i + 1]) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
}
+13-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
class Solution:
2-
def checkPossibility(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: bool
6-
"""
7-
if len(nums) < 2:
8-
return True
9-
count = 0
10-
for i in range(1, len(nums)):
11-
if nums[i] < nums[i - 1]:
12-
if count == 1:
13-
return False
14-
if not (
15-
i + 1 == len(nums)
16-
or nums[i + 1] >= nums[i - 1]
17-
or i - 2 < 0
18-
or nums[i - 2] < nums[i]
19-
):
20-
return False
21-
else:
22-
count = 1
2+
def checkPossibility(self, nums: List[int]) -> bool:
3+
def is_sorted(nums: List[int]) -> bool:
4+
return all(a <= b for a, b in pairwise(nums))
5+
6+
n = len(nums)
7+
for i in range(n - 1):
8+
a, b = nums[i], nums[i + 1]
9+
if a > b:
10+
nums[i] = b
11+
if is_sorted(nums):
12+
return True
13+
nums[i] = nums[i + 1] = a
14+
return is_sorted(nums)
2315
return True

0 commit comments

Comments
 (0)
Please sign in to comment.