当前位置:网站首页>leetcode 665. Non-decreasing Array
leetcode 665. Non-decreasing Array
2022-07-31 18:19:00 【InfoQ】
I. Topic effect
tag: greedy
https://leetcode.cn/problems/non-decreasing-array
Give you an integer array of length n nums , please judge whether the array can become a non-decreasing array with a change of at most 1 elementsequence.
We define a non-decreasing sequence like this: For any i (0 <= i <= n-2) in the array, nums[i] <=nums[i + 1].
Example 1:
Input: nums = [4,2,3]Output: true Explanation: You can do this by putting the first 4becomes 1 to make it a non-decreasing sequence.
Example 2:
Input: nums = [4,2,1]Output: false Explanation: You cannotChanges an element to a non-decreasing sequence.
Tips:
- n == nums.length
- 1 <= n <= 104
- -105 <= nums[i] <= 105
Second, problem-solving ideas
There is only one chance to modify a certain number at most, ask whether the array can be changed into a non-decreasing array.Example
Example 1: 4, 2, 3
Example 2:-1, 4,
2
, 3
Example 3: 2, 3, 3, 2, 4 When the following number is less than the previous number, for example, 2 in example 1 is less than 4, then you can modifyModify the preceding number to change 4 to 2 or modify the following number to change 2 to 4.We need to find this rule and determine which number to modify is actually related to the size of the previous number:
If the previous number does not exist, in example 1, there is no number in front of 4, we directlyModify the previous number to the current number 2;
If the previous number exists and is smaller than the current number, in example 2, we need to modify the previous number 4 to the current number 2;
If the previous number exists and is greater than the current number, in example 3, we need to modify the current number 2 to the previous number 3.
Because there is only one chance to modify, so use a variable cnt, initialize it to 1, after modifying the number, cnt will be decremented by 1. When you need to modify it next time, if cnt is 0, return false directly.Returns true after the traversal is complete.
Three, problem solving method
3.1 Java implementation
public class Solution {
public boolean checkPossibility(int[] nums) {
int cnt = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
if(cnt == 0) {
return false;
}
cnt--;
if (i == 1) {
nums[i - 1] =nums[i];
} else if (nums[i] >= nums[i - 2]) {
nums[i - 1] = nums[i];
} else {
nums[i] = nums[i - 1];
}
}
}
return true;
}
}
Four, Summary Notes
- 2022/7/31 The weather is very hot today
边栏推荐
- Apache EventMesh 分布式事件驱动多运行时
- 京东按关键字搜索商品 API
- 认识异常 (看完这篇你就懂了)
- 研发过程中的文档管理与工具
- 最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
- 你辛辛苦苦写的文章可能不是你的原创
- 【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
- Last write wins (discards concurrent writes)
- ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
猜你喜欢
随机推荐
【luogu P8326】Fliper(图论)(构造)(欧拉回路)
Last write wins (discards concurrent writes)
基于WPF重复造轮子,写一款数据库文档管理工具(一)
Istio介绍
Write a database document management tool based on WPF repeating the wheel (1)
After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
JD.com searches for products by keyword API
使用 Flutter 和 Firebase 制作!计数器应用程序
mysql的备份表的几种方法
Flink_CDC搭建及简单使用
多数据中心操作和检测并发写入
GateWay实现负载均衡
C# 之 扑克游戏 -- 21点规则介绍和代码实现
MySQL---子查询
你辛辛苦苦写的文章可能不是你的原创
Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
京东按关键字搜索商品 API
[pytorch] pytorch automatic derivation, Tensor and Autograd
多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
架构师04-应用服务间加密设计和实践