엔지니어 게시판
LeetCode 솔루션 분류

[6/25] 665. Non-decreasing Array

컨텐츠 정보

본문

Medium
3709670Add to ListShare

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 first 4 to 1 to 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.length
  • 1 <= n <= 104
  • -105 <= nums[i] <= 105

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 193 ms, faster than 92.63% of Python3 online submissions for Non-decreasing Array.
Memory Usage: 15.4 MB, less than 5.92% of Python3 online submissions for Non-decreasing Array.
class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        
        modified = False
        
        nums = [-10e5] + nums + [10e5]
        for i in range(len(nums[1:])):
            if nums[i] <= nums[i+1]: continue
            if modified: return False

            if nums[i-1]<=nums[i+1]: nums[i] = nums[i+1]
            else: nums[i+1] = nums[i]
            modified = True
         
        return True
            
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 150 명
  • 오늘 방문자 830 명
  • 어제 방문자 5,475 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,092 명
알림 0