LeetCode # Array # Easy # 665. Non-decreasing Array

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

题意:给定一个数组,只调整一个元素的条件下,该数组能否变成一个递增的数组,

思路:(参考)用贪心算法,当发现nums[i-1]>nums[i]时,我们希望通过改变一个值,使局部递增。

当没有nums[i-2],或者nums[i-2] <= nums[i]时,将nums[i-1]=nums[i],这样使得局部有序;

当nums[i-2]>nums[i]时,将nums[i-1]=nums[i],因为nums[i-1]>=nums[i-2],所以做修改后,局部有序;

修改完一次后,如果还有降序的元素,则该数组不符合条件。

 1 class Solution {
 2     public boolean checkPossibility(int[] nums) {
 3         int cnt = 0,len = nums.length; ;
 4         for(int i = 1; i < len && cnt<=1 ; i++){
 5             if(nums[i-1] > nums[i]){
 6                 cnt++;
 7                 if(i-2<0 || nums[i-2] <= nums[i])nums[i-1] = nums[i];
 8                 else nums[i] = nums[i-1];
 9             }
10         }
11         return cnt<=1;
12     }
13 }

原文地址:https://www.cnblogs.com/DongPingAn/p/9038449.html

时间: 2024-11-09 03:14:29

LeetCode # Array # Easy # 665. Non-decreasing Array的相关文章

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

LeetCode | 1408. String Matching in an Array数组中的字符串匹配【Python】

LeetCode 1408. String Matching in an Array数组中的字符串匹配[Easy][Python][字符串] Problem LeetCode Given an array of string words. Return all strings in words which is substring of another word in any order. String words[i] is substring of words[j], if can be o

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

LeetCode--122、167、169、189、217 Array(Easy)

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

896. Monotonic Array - Easy

An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if