Skip to content

Commit 6797afd

Browse files
author
lucifer
committed
feat: $665
1 parent e53ac44 commit 6797afd

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
214214
- [0437. 路径总和 III](./problems/437.path-sum-iii.md)
215215
- [0455. 分发饼干](./problems/455.AssignCookies.md)
216216
- [0575. 分糖果](./problems/575.distribute-candies.md)
217+
- [0665. 非递减数列](./problems/665.non-decreasing-array.md) 🆕
217218
- [821. 字符的最短距离](./problems/821.shortest-distance-to-a-character.md) 91
218219
- [0874. 模拟行走机器人](./problems/874.walking-robot-simulation.md)
219220
- [1128. 等价多米诺骨牌对的数量](./problems/1128.number-of-equivalent-domino-pairs.md) 🆕

Diff for: SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
- [0437. 路径总和 III](problems/437.path-sum-iii.md)
8686
- [0455. 分发饼干](problems/455.AssignCookies.md)
8787
- [0575. 分糖果](problems/575.distribute-candies.md)
88+
- [0665. 非递减数列](./problems/665.non-decreasing-array.md) 🆕
8889
- [821. 字符的最短距离](problems/821.shortest-distance-to-a-character.md) 91
8990
- [0874. 模拟行走机器人](problems/874.walking-robot-simulation.md)
9091
- [1128. 等价多米诺骨牌对的数量](./problems/1128.number-of-equivalent-domino-pairs.md) 🆕

Diff for: problems/665.non-decreasing-array.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
## 题目地址(665. 非递减数列)
2+
3+
https://leetcode-cn.com/problems/non-decreasing-array/
4+
5+
## 题目描述
6+
7+
```
8+
给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列。
9+
10+
我们是这样定义一个非递减数列的: 对于数组中所有的 i (0 <= i <= n-2),总满足 nums[i] <= nums[i + 1]。
11+
12+
 
13+
14+
示例 1:
15+
16+
输入: nums = [4,2,3]
17+
输出: true
18+
解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列。
19+
20+
21+
示例 2:
22+
23+
输入: nums = [4,2,1]
24+
输出: false
25+
解释: 你不能在只改变一个元素的情况下将其变为非递减数列。
26+
27+
28+
 
29+
30+
说明:
31+
32+
1 <= n <= 10 ^ 4
33+
- 10 ^ 5 <= nums[i] <= 10 ^ 5
34+
```
35+
36+
## 前置知识
37+
38+
- 数组
39+
- 贪心
40+
41+
## 公司
42+
43+
- 暂无
44+
45+
## 思路
46+
47+
这道题简单就简单在它限定了**在 最多 改变  1 个元素的情况下**,如果不限定这个条件,让你求最少改变多少个数字,就有点难度了。
48+
49+
对于这道题来说,我们可以从左到右遍历数组 A, 如果 A[i] < A[i - 1],我们找到了一个递减对。遍历过程中的递减对个数大于 1,则可提前返回 False。
50+
51+
于是,大家可能写出如下代码:
52+
53+
```py
54+
class Solution:
55+
def checkPossibility(self, A: List[int]) -> bool:
56+
ans = 0
57+
for i in range(1, len(A)):
58+
if A[i] < A[i - 1]:
59+
if ans == 1: return False
60+
ans += 1
61+
return True
62+
```
63+
64+
上面代码是有问题的,问题在于类似 `[3,4,2,3]` 的测试用例会无法通过。问题在于递减对的计算方式有问题。
65+
66+
对于 `[3,4,2,3]` 来说,其递减对不仅仅有 (4,2)。其实应该还包括 (4,3)。 这提示我们在这个时候应该将 2 修改为不小于前一项的数,也就是 4,此时数组为 `[3,4,4,3]` 。这样后续判断就会多一个(4,3) 递减对。
67+
68+
而如果是 `[3,4,3,3]`,在这个例子中应该将**前一项**修改为 3,即 [3,3,3,3],因为**末尾数字越小,对形成递增序列越有利,这就是贪心的思想**。代码上,我们没有必要修改前一项,而是假设 ta 已经被修改了即可。
69+
70+
大家可以继续找几个测试用例,发现一下问题的规律。比如我找的几个用例: `[4,2,3] [4,2,1] [1,2,1,2] [1,1,1,] []`。这样就可以写代码了。
71+
72+
## 关键点
73+
74+
- 考虑各种边界情况,贪心改变数组的值
75+
76+
## 代码
77+
78+
- 语言支持:Python3
79+
80+
Python3 Code:
81+
82+
```python
83+
84+
class Solution(object):
85+
def checkPossibility(self, A):
86+
N = len(A)
87+
count = 0
88+
for i in range(1, N):
89+
if A[i] < A[i - 1]:
90+
count += 1
91+
if count > 1:
92+
return False
93+
# [4,2,3] [4,2,1] [1,2,1,2] [1,1,1,] []
94+
if i >= 2 and A[i] < A[i - 2]:
95+
A[i] = A[i - 1]
96+
97+
return True
98+
99+
```
100+
101+
**复杂度分析**
102+
103+
令 n 为数组长度。
104+
105+
- 时间复杂度:$O(n)$
106+
- 空间复杂度:$O(1)$
107+
108+
## 相关专题
109+
110+
- 最长上升子序列
111+
112+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
113+
114+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
115+
116+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
117+
118+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
119+
120+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)