파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 6. 25. 23:45

https://leetcode.com/problems/non-decreasing-array/

 

Non-decreasing Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제를 처음에 잘 못 이해해서 sorted를 써서 풀었는데
그냥 순차적으로 하나씩 접근해 가면서 값 비교하면서 풀면 되는 문제였다.

class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        cnt = 0
        for i in range(1, len(nums)):
            if nums[i] < nums[i-1]:
                if cnt == 1:
                    return False
                cnt += 1
                if i >= 2 and nums[i-2] > nums[i]:
                    nums[i] = nums[i-1]
        return True
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.06.27
Today's Challenge  (0) 2022.06.26
Today's Challenge  (0) 2022.06.24
Today's Challenge  (0) 2022.06.23
Today's Challenge  (0) 2022.06.22
Comments