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 | 31 |
Tags
- Real or Not? NLP with Disaster Tweets
- Kaggle
- 맥북
- Baekjoon
- 편스토랑
- 자연어처리
- AI 경진대회
- 프로그래머스 파이썬
- Git
- 프로그래머스
- ChatGPT
- programmers
- 금융문자분석경진대회
- hackerrank
- dacon
- 편스토랑 우승상품
- PYTHON
- gs25
- ubuntu
- 더현대서울 맛집
- SW Expert Academy
- github
- 백준
- 코로나19
- 우분투
- Docker
- 데이콘
- 캐치카페
- leetcode
- 파이썬
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
'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 |
Comments