当前位置:网站首页>Leetcode 665 non decreasing array (greedy)
Leetcode 665 non decreasing array (greedy)
2022-06-11 01:44:00 【_ TCgogogo_】
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Example 1:
Input: nums = [4,2,3] Output: true Explanation: You could modify the first4to1to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1] Output: false Explanation: You can't get a non-decreasing array by modify at most one element.
Constraints:
n == nums.length1 <= n <= 104-105 <= nums[i] <= 105
Topic link :https://leetcode.com/problems/non-decreasing-array/
The main idea of the topic : Give an array , Change at most one number and ask if you can make it monotonous
Topic analysis : When encountering a group of continuous decreasing , hypothesis nums[i] < nums[i - 1], There are two options :
1. take nums[i] Bigger , The optimal solution to guarantee monotonicity is to nums[i-1] Assign to nums[i]
2. take nums[i-1] smaller , The optimal solution to guarantee monotonicity is to nums[i - 2] Assign a value to nums[i - 1], if i be equal to 1, The assignment is nums[i] that will do
You might as well choose the scheme first 2, After modification, you need to judge whether the modified value is less than or equal to nums[i], If not, only the scheme can be adopted 1, If there is a decline after that, it cannot be completed
0ms, Time beats 100%
class Solution {
public boolean checkPossibility(int[] nums) {
int n = nums.length;
if (n < 3) {
return true;
}
boolean change = false;
for (int i = 1; i < n; i++) {
if (nums[i] < nums[i - 1]) {
if (!change) {
change = true;
int tmp = i - 2 >= 0 ? nums[i - 2] : nums[i];
if (tmp > nums[i]) {
nums[i] = nums[i - 1];
} else {
nums[i - 1] = tmp;
}
} else {
return false;
}
}
}
return true;
}
}边栏推荐
- 对象存储 S3 在分布式文件系统中的应用
- Docking of express bird system
- Shenzhen Nanshan District specialized, special and new enterprise application conditions, with a subsidy of 100000-500000 yuan
- Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system
- Tencent Cloud Database tdsql - Dajia comments | The Past, Present and Future of basic software
- SAS判别分析(Bayes准则和proc discrim过程)
- Inventory management and strategy mode
- SAS聚类分析(系统聚类cluster,动态聚类fastclus,变量聚类varclus)
- CSRF attack
- About mobx
猜你喜欢

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture

Threejs: streamer effect encapsulation

SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)

记录打包GoogleChrome浏览器插件

Multi interest recall model practice | acquisition technology

2.0 detailed explanation of ROS and Px4 communication

Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system

Sealem Finance打造Web3去中心化金融平台基础设施

How to use user-defined annotation for parameter verification
![[geometric vision] 4.2 piecewise linear transformation](/img/9e/ad010e0b55c88f2c0244442ae20fb3.jpg)
[geometric vision] 4.2 piecewise linear transformation
随机推荐
中间件_Redis_06_Redis的事务
2.2、ROS+PX4仿真多点巡航飞行----正方形
[Li mu] how to read papers [intensive reading of papers]
[path planning] week 1: hodgepodge
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
Introduction to the application process of China Patent Award, with a subsidy of 1million yuan
如何下载网页照片
Leetcode linked list queue stack problem
Leetcode binary tree problem
[chess life] 01 life is like chess
Record the packaging of the googlechrome browser plug-in
Introduction to China patent award policy support, with a subsidy of 1million yuan
Conda安装Pytorch后numpy出现问题
1.3 introduction to ROS UAV
Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler
SQL question brushing and sorting in leetcode of partial deduction
Shenzhen Nanshan District specialized, special and new enterprise application conditions, with a subsidy of 100000-500000 yuan
About mobx
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
Yunna PDA wireless fixed assets inventory management system