当前位置:网站首页>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;
}
}边栏推荐
- MultipartFile和File互转工具类
- Threejs: streamer effect encapsulation
- After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?
- 2.1 ros+px4 simulation - Fixed Point flight control
- MATLAB随机函数汇总
- From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture
- Middleware_ Redis_ 06_ Redis transactions
- Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
- Function of barcode fixed assets management system, barcode management of fixed assets
- Yunna PDA wireless fixed assets inventory management system
猜你喜欢

1.3 ROS 无人机简介

Sealem finance builds Web3 decentralized financial platform infrastructure

Threejs: how to get the boundingbox of geometry?

云呐|PDA无线固定资产盘点管理系统

Leetcode linked list queue stack problem

IRS application release 16: H5 application design guide

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

Current limiting and download interface request number control

项目_基于网络爬虫的疫情数据可视化分析

How to download web photos
随机推荐
What is the C-end and what is the b-end? Let me tell you
threejs:两点坐标绘制贝赛尔曲线遇到的坑
Middleware_ Redis_ 06_ Redis transactions
[geometric vision] 4.2 piecewise linear transformation
Multipartfile and file interoperation tool classes
Middleware_ Redis_ 05_ Persistence of redis
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
threejs:流光效果封装
复利的保险理财产品怎么样?可以买吗?
LeetCode 1029 Two City Scheduling (dp)
How much is the bonus of China Patent Award, with a subsidy of 1million yuan
IRS application release 15: application security self test guide
Leetcode 1814 count nice pairs in an array (recommended by map)
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
Project_ Visual analysis of epidemic data based on Web Crawler
PX4装机教程(六)垂起固定翼(倾转)
SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
2022.6.6-----leetcode. seven hundred and thirty-two
MATLAB数组其他常见操作笔记