Post

leetcode(리트코드)-1909 Remove One Element to Make the Array Strickly Increasing(python)

leetcode 1909 - Remove One Element to Make the Array Strictly Increasing 문제입니다.

1. 문제

https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • nums가 들어옵니다. 해당 문자열에서 1개만 지웠을 때 오름차순으로 정렬된 리스트로 만들 수 있는 지 확인해야합니다.
  • 2가지 상태가 있습니다. 일단 i-1(이전 요소)보다 i가 작은 경우에서 i-2보다도 작은 경우라면 False입니다. 이전요소보다 i가 작은 경우에는 i를 i-1로 갱신해주면 됩니다.

5. code

코드설명

python

1
2
3
4
5
6
7
8
9
10
class Solution:
    def canBeIncreasing(self, nums: List[int]) -> bool:
        res = 0 
        for i in range(1,len(nums)):
            if nums[i-1] >= nums[i] : 
                res+=1
                if i >1 and nums[i-2] >= nums[i] : 
                    nums[i] = nums[i-1]
        return res < 2  
            

6. 결과 및 후기, 개선점

필요시 c++로 짜드리겠습니다.

This post is licensed under CC BY 4.0 by the author.