GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/665_Non-decreasing_Array.py at master · qiyuangong/leetcode

Expand file tree

/

665_Non-decreasing_Array.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
38 lines (37 loc) · 1.32 KB
/

665_Non-decreasing_Array.py

Copy path

File metadata and controls

38 lines (37 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
32
33
34
35
36
37
38
class Solution(object):
# def checkPossibility(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# pos = None
# # Check if there are more than 2 broken point
# # record first index
# for i in range(len(nums) - 1):
# if nums[i] > nums[i + 1]:
# if pos is not None:
# return False
# pos = i
# if pos is None or pos == 0 or pos == len(nums) - 2:
# return True
# # Remove pos or remove pos + 1
# return (nums[pos - 1] <= nums[pos + 1] or nums[pos] <= nums[pos + 2])
def checkPossibility(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# https://leetcode.com/problems/non-decreasing-array/discuss/106826/JavaC%2B%2B-Simple-greedy-like-solution-with-explanation
broken_num = 0
for i in range(len(nums) - 1):
if (nums[i] > nums[i + 1]):
broken_num += 1
if broken_num >= 2:
return False
if (i - 1 < 0 or nums[i - 1] <= nums[i + 1]):
# Remove i
nums[i] = nums[i + 1]
else:
# Remove i + 1
nums[i + 1] = nums[i]
return True
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.