Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- PYTHON
- 금융문자분석경진대회
- ubuntu
- ChatGPT
- 자연어처리
- Docker
- 캐치카페
- SW Expert Academy
- Git
- 더현대서울 맛집
- 맥북
- gs25
- Real or Not? NLP with Disaster Tweets
- Kaggle
- dacon
- 우분투
- Baekjoon
- 프로그래머스
- 편스토랑 우승상품
- 데이콘
- 프로그래머스 파이썬
- hackerrank
- programmers
- leetcode
- 파이썬
- AI 경진대회
- 백준
- 편스토랑
- 코로나19
- github
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 665. Non-decreasing Array (Python) 본문
728x90
반응형

Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 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:
- 1 <= n <= 10 ^ 4
- - 10 ^ 5 <= nums[i] <= 10 ^ 5
Solution
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
if len(nums) < 2:
return True
cnt=0
for i in range( 1, len(nums) ):
if nums[i-1]>nums[i]:
cnt = cnt + 1
if i == 1 or nums[i-2] <= nums[i]:
nums[i-1] = nums[i]
else:
nums[i] = nums[i-1]
if cnt > 1:
return False
return True



SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 709. To Lower Case (Python) (0) | 2020.08.15 |
---|---|
[leetCode] 263. Ugly Number (Python) (1) | 2020.08.14 |
[leetCode] 344. Reverse String (Python) (0) | 2020.08.13 |
[leetCode] 121. Best Time to Buy and Sell Stock (Python) (0) | 2020.08.13 |
[leetCode] 392. Is Subsequence (Python) (0) | 2020.08.13 |