Non-decreasing Array

    这道题为简单题,但是做的过程中忽略了一些特殊情况,最后还是做出来了,怪说不得这道题的通过率才21%左右

  题目:

    

  思路:

    我设置了两个变量,prev代表上一个元素,prev_prev代表上上个元素,代码的大致思路就是如果有两次(因为我把两个值设置为无穷小,所以第一次num就是自动加1,但是这一次不算)该元素比上一个prev小,那么就返回False,否则True,在这个过程中最难的就是第一次出现的时候如何改变prev和prev_prev的值。如果上上个元素小于i,那么就把上个和上上个元素都变为i,prev_prev == float(‘Inf’) 这个条件是由于当时没考虑到长度小于3时才加的,其他情况就把上个和上上个元素都变为i元素的上个元素值

  代码:

class Solution(object):
    def checkPossibility(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        prev_prev = float(‘inf‘)
        prev = float(‘inf‘)
        num = 0
        for i in nums:
            if i < prev:
                num += 1
                if num == 1: prev = i
            else:
                prev_prev = prev
                prev = i
            if num == 2:
                if prev_prev == float(‘inf‘) or prev_prev <= i:
                    prev_prev = i
                    prev = i
                else:
                    prev_prev = prev
            if num == 3:
                return False
        return True

    

时间: 2024-08-09 23:54:39

Non-decreasing Array的相关文章

Coursera Algorithms week1 Interview Questions: Search in a bitonic array

题目要求: An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer

CodeForces 831A Unimodal Array

Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of thi

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay

Round #424 A. Unimodal Array

Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of thi

896. Monotonic Array

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

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

[Swift]LeetCode896. 单调数列 | Monotonic Array

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

[LeetCode] 896. Monotonic Array 单调数组

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

PHP Warning: array_multisort(): Array sizes are inconsistent

array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序. 遇到这报错是两个数组对比不一致导致的, 如果是一维数组与二维数组进行排序可以用以下方法解决: 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列.具体代码如下: 复制代码代码如下: $ages = array();foreach ($users as $user) {    $ages[]

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把