[LeetCode] Non-decreasing Array 非递减数列

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

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

Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array. 

Example 2:

Input: [4,2,1]
Output: False
Explanation: You can't get a non-decreasing array by modify at most one element.

Note: The n belongs to [1, 10,000].

这道题给了我们一个数组,说我们最多有1次修改某个数字的机会,问能不能将数组变为非递减数组。题目中给的例子太少,不能覆盖所有情况,我们再来看下面三个例子:

4,2,3

-1,4,2,3

2,3,3,2,4

我们通过分析上面三个例子可以发现,当我们发现后面的数字小于前面的数字产生冲突后,有时候需要修改前面较大的数字(比如前两个例子需要修改4),有时候却要修改后面较小的那个数字(比如前第三个例子需要修改2),那么有什么内在规律吗?是有的,判断修改那个数字其实跟再前面一个数的大小有关系,首先如果再前面的数不存在,比如例子1,4前面没有数字了,我们直接修改前面的数字为当前的数字2即可。而当再前面的数字存在,并且小于当前数时,比如例子2,-1小于2,我们还是需要修改前面的数字4为当前数字2;如果再前面的数大于当前数,比如例子3,3大于2,我们需要修改当前数2为前面的数3。这是修改的情况,由于我们只有一次修改的机会,所以用一个变量cnt,初始化为1,修改数字后cnt自减1,当下次再需要修改时,如果cnt已经为0了,直接返回false。遍历结束后返回true,参见代码如下:

class Solution {

public:
    bool checkPossibility(vector<int>& nums) {
        int cnt = 1, n = nums.size();
        for (int i = 1; i < n; ++i) {
            if (nums[i] < nums[i - 1]) {
                if (cnt == 0) return false;
                if (i == 1 || nums[i] >= nums[i - 2]) nums[i - 1] = nums[i];
                else nums[i] = nums[i - 1];
                --cnt;
            }
        }
        return true;
    }
};

 参考资料:

https://discuss.leetcode.com/topic/101144/java-c-simple-greedy-like-solution-with-explanation

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Non-decreasing Array 非递减数列

,如需转载请自行联系原博主。

时间: 2024-08-01 02:06:27

[LeetCode] Non-decreasing Array 非递减数列的相关文章

C语言实现两个递减数列中寻找某一个数_C 语言

本文实例讲述了C语言实现两个递减数列中寻找某一个数的方法,分享给大家供大家参考之用.具体方法如下: 通常来说这道题算二分查找法中非常有难度的一题了. 题目如下: 一个数组是由一个递减数列左移若干位形成,比如{4, 3, 2, 1, 6, 5}是由{6, 5, 4, 3, 2, 1}左移两位,在这种数组中查找某一个数. 实现代码如下: int array[] = {4, 3, 2, 1, 6, 5}; const int size = sizeof array / sizeof *array; i

[LeetCode]189.Rotate Array

题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve this

[LeetCode] Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Fo

580A. Kefa and First Steps (Codeforces Round #321 (Div. 2))

A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 

关于数据结构(c语言版)线性表的问题

问题描述 关于数据结构(c语言版)线性表的问题 写完线性表实验代码后,有些错误 不会调试 求大神帮帮忙! ps.错误截图:图片说明 代码: #include #include #include #define LIST_INIT_SIZE 100 //线性表存储空间的初始分量 #define LISTINCREMENT 10 //线性表存储空间的分配增量 typedef struct{ ElemType* elem; //存储空间基址 int length; //当前长度 int listsiz

[LeetCode]18.4Sum

[题目] 4Sum  Total Accepted: 3493 Total Submissions: 16137My Submissions Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. No

[LeetCode]15.3Sum

[题目] 3Sum  Total Accepted: 6032 Total Submissions: 35898My Submissions Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a tri

经典算法(9) 从归并排序到数列的逆序数对(微软笔试题)

首先来看看原题 微软2010年笔试题 在一个排列中,如果一对数的前后位置与大小顺序相反 ,即前面的数大于后面的数,那么它们就称为一个逆序数对.一个排列中逆序的总数就称为这个排列的逆序 数.如{2,4,3,1}中,2和1,4和3,4和1,3和1是逆序数对,因此整个数组的逆序数对个数为4,现在给定 一数组,要求统计出该数组的逆序数对个数. 计算数列的逆序数对个数最简单的方便就最从前向后依 次统计每个数字与它后面的数字是否能组成逆序数对.代码如下: #include <stdio.h> int ma

Array 与 ArrayList区别

问题描述 能否简单代码实例Array与ArrayList的区别? 解决方案 解决方案二:http://wenku.baidu.com/view/2cb8d572f242336c1eb95e94.html结贴吧解决方案三:[Array和ArrayList的区别]#1.Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大小),而ArrayList可以只是先声明.如:int[]array=newarray[3];或int[]array={1,2,3};或ArrayListmyList=