当前位置:网站首页>Leetcode (665) -- non decreasing column
Leetcode (665) -- non decreasing column
2022-06-28 14:30:00 【SmileGuy17】
Leetcode(665)—— Non decreasing Columns
subject
Give you a length of n Array of integers for nums , Please judge in most change 1 In the case of elements , Whether the subtracted array can become a non recursive column .
This is how we define a non decreasing column : For any i (0 <= i <= n-2), Always satisfied nums[i] <= nums[i + 1].
Example 1:
Input : nums = [4,2,3]
Output : true
explain : You can do this by putting the first one 4 become 1 To make it a non decreasing column .
Example 2:
Input : nums = [4,2,1]
Output : false
explain : You can't change one element to a non decreasing column .
Tips :
- n == nums.length
- 1 <= n <= 104
- -105 <= nums[i] <= 105
Answer key
Method 1 : Greedy Algorithm
Ideas
First, consider the following questions : To make an array nums \textit{nums} nums Become a non decreasing column , How many... Are there at most in the array i i i Satisfy nums [ i ] > nums [ i + 1 ] \textit{nums}[i]>\textit{nums}[i+1] nums[i]>nums[i+1]?
At most one .
Suppose there are two different subscripts i i i, j j j Satisfy the above inequality , Might as well set i < j i<j i<j.
- if i + 1 < j i+1<j i+1<j, No matter how it is modified nums [ i ] \textit{nums}[i] nums[i] or nums [ i + 1 ] \textit{nums}[i+1] nums[i+1], Will not affect nums [ j ] \textit{nums}[j] nums[j] And nums [ j + 1 ] \textit{nums}[j+1] nums[j+1] Size relationship between ; modify nums [ j ] \textit{nums}[j] nums[j] or nums [ j + 1 ] \textit{nums}[j+1] nums[j+1] Also in the same way . therefore , In this case , We can't put nums \textit{nums} nums Become a non decreasing column .
- if i + 1 = j i+1=j i+1=j, Then there are nums [ i ] > nums [ i + 1 ] > nums [ i + 2 ] \textit{nums}[i]>\textit{nums}[i+1]>\textit{nums}[i+2] nums[i]>nums[i+1]>nums[i+2]. similarly , No matter how you modify one of the numbers , Can not make these three numbers into nums [ i ] ≤ nums [ i + 1 ] ≤ nums [ i + 2 ] \textit{nums}[i]\le\textit{nums}[i+1]\le\textit{nums}[i+2] nums[i]≤nums[i+1]≤nums[i+2] The size of the relationship .
Is it enough to meet this condition ? Does not , For arrays that meet this condition [ 3 , 4 , 1 , 2 ] [3,4,1,2] [3,4,1,2] for , No matter how you modify it, you can't turn it into a non decreasing column .
therefore , If you find a satisfaction nums [ i ] > nums [ i + 1 ] \textit{nums}[i]>\textit{nums}[i+1] nums[i]>nums[i+1] Of i i i, In the revision nums [ i ] \textit{nums}[i] nums[i] or nums [ i + 1 ] \textit{nums}[i+1] nums[i+1] after , We need to check nums \textit{nums} nums Whether it becomes a non decreasing column .
We can nums [ i ] \textit{nums}[i] nums[i] Change to less than or equal to nums [ i + 1 ] \textit{nums}[i+1] nums[i+1] Number of numbers , However, due to the need to ensure nums [ i ] \textit{nums}[i] nums[i] Not less than the number before it , nums [ i ] \textit{nums}[i] nums[i] The bigger the better , So will nums [ i ] \textit{nums}[i] nums[i] Modified into nums [ i + 1 ] \textit{nums}[i+1] nums[i+1] It's the best ; Empathy , about nums [ i + 1 ] \textit{nums}[i+1] nums[i+1], Modified into nums [ i ] \textit{nums}[i] nums[i] It's the best .
Can I traverse only once nums \textit{nums} nums Array? ?
modify nums [ i ] \textit{nums}[i] nums[i] by nums [ i + 1 ] \textit{nums}[i+1] nums[i+1] after , And you need to make sure nums [ i − 1 ] ≤ nums [ i ] \textit{nums}[i-1]\le\textit{nums}[i] nums[i−1]≤nums[i] Still established , namely nums [ i − 1 ] ≤ nums [ i + 1 ] \textit{nums}[i-1]\le\textit{nums}[i+1] nums[i−1]≤nums[i+1], If this inequality does not hold, the entire array must not be non decreasing , It needs to be modified nums [ i + 1 ] \textit{nums}[i+1] nums[i+1] by nums [ i ] \textit{nums}[i] nums[i]. After modification , Then judge the size relationship of subsequent numbers . Statistics in traversal nums [ i ] > nums [ i + 1 ] \textit{nums}[i]>\textit{nums}[i+1] nums[i]>nums[i+1] The number of times , If it is more than once, it can be returned directly false \text{false} false.
Code implementation
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
// 1. Find the first pair of values that cause the sequence to be non decreasing ( The former is greater than the latter ), Return if not found true
// 2. Find the first pair of values that cause the sequence to be non decreasing , Judge whether the former of the first value is less than or equal to the second value , if true Continued to , if false be
// Determine whether the first value is less than or equal to the latter of the second value , if true Continued to , Otherwise return to false
// 3. Then judge whether the sequences are all non decreasing columns , That is, whether we can find the second pair of values that cause the sequence to be non decreasing , Found return false, Otherwise return to true
int size = nums.size(), non_value = 0; // non_value Indicates the number of places that cause the sequence to be non decreasing
if(size <= 2) return true; // If it is less than or equal to 2, It must be able to change at most 1 In the case of elements, the array becomes a non - decreasing column
for(int n = 1; n < size; n++){
if(nums[n-1] > nums[n]){
// Two non decreasing values
if(++non_value <= 1) {
// Also note whether it is n-1 Whether it is the first value and n Whether it is a special case of the last value
if(n == 1 || nums[n-2] <= nums[n] || n == size-1 || nums[n-1] <= nums[n+1]) continue;
else return false;
}else return false;
}
}
return true;
}
};
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among n n n It's an array nums \textit{nums} nums The length of .
Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index
- 2022年焊工(技师)考试题库模拟考试平台操作
- Based on asp Net based document retrieval system
- Can your code talk? (upper)
- 单一职责原则
- How to count dimensions of foreign trade E-mail Promotion
- 哪个证券公司最大最安全 怎么办理开户最安全
- Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)
- 【二叉树】从叶结点开始的最小字符串
- Double buffer drawing
猜你喜欢

【中移芯昇】5. spi接口测试tf卡

PC博物馆-熟悉又陌生的懵懂年代

After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time

Why can't Bert completely kill the BM25??

运行近20年,基于Win 98的火星探测器软件迎来首次升级

Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation

flutter 系列之:flutter 中的 offstage

干货 | 科研人的KPI怎么算,H指数和G指数是什么

接雨水系列问题

物联网低代码平台常用《组件介绍》
随机推荐
ArcGIS vector center point generates rectangle and cuts TIF image for deep learning sample training
接雨水系列问题
Robot range of motion (DFS)
证券公司和银行哪个更安全 怎么办理开户最安全
CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
Four methods of thread termination
Single responsibility principle
Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
Double buffer drawing
Introduction to common components of IOT low code platform
Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
2022中式烹调师(高级)试题及在线模拟考试
Work study management system based on ASP
原生JS 实现页面元素的拖动 拖拽
The time schedule for the soft test in the second half of 2022 has been determined!
锐捷交换机配置ssh password登录命令[通俗易懂]
Configuration file encryption (simple use of jasypt)
Which securities company is the largest and safest? How to open an account is the safest
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)