当前位置:网站首页>Leetcode simple problem delete an element to strictly increment the array
Leetcode simple problem delete an element to strictly increment the array
2022-07-03 04:31:00 【·Starry Sea】
subject
I'll give you a subscript from 0 The starting array of integers nums , If just Delete One After the element , Array Strictly increasing , So please go back to true , Otherwise return to false . If the array itself is strictly incremented , Please return, too true .
Array nums yes Strictly increasing For the definition of : For any subscript 1 <= i < nums.length All satisfied with nums[i - 1] < nums[i] .
Example 1:
Input :nums = [1,2,10,5,7]
Output :true
explain : from nums Remove the subscript from the 2 Situated 10 , obtain [1,2,5,7] .
[1,2,5,7] It's strictly incremental , So back true .
Example 2:
Input :nums = [2,3,1,2]
Output :false
explain :
[3,1,2] Is to delete the subscript 0 The result of the element .
[2,1,2] Is to delete the subscript 1 The result of the element .
[2,3,2] Is to delete the subscript 2 The result of the element .
[2,3,1] Is to delete the subscript 3 The result of the element .
No result array is strictly incremented , So back false .
Example 3:
Input :nums = [1,1,1]
Output :false
explain : The result of deleting any element is [1,1] .
[1,1] Not strictly incremental , So back false .
Example 4:
Input :nums = [1,2,3]
Output :true
explain :[1,2,3] It has been strictly increasing , So back true .
Tips :
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
source : Power button (LeetCode)
Their thinking
A simple direction is to traverse the array first , Find the point where the elements in the array do not conform to strict increment , Then modify the array by analyzing the situation , Of course, you can only delete it once , Finally, another round of judgment .
class Solution:
def canBeIncreasing(self, nums: List[int]) -> bool:
i=1
n=len(nums)
while i<len(nums):
if nums[i]>nums[i-1]:
i+=1
else: # If the current point makes the array not strictly single increment
if i-2>=0: # Check the second point before the current element
if nums[i]>nums[i-2]: # If the current point and the previous second point form a strict increment , Then delete the first point in front of the current element
del nums[i-1]
else: # Otherwise, delete the current element
del nums[i]
else: # If there is only one element in front , Then delete the current point
del nums[i-1]
break
if n==len(nums): # There was no deletion , Strict single increment of the original array
return True
for i in range(1,len(nums)): # Whether to strictly add after a deletion
if nums[i]<=nums[i-1]:
return False
return True

边栏推荐
- FFMpeg filter
- How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- vulnhub HA: Natraj
- Youdao cloud notes
- Kubernetes源码分析(一)
- Xrandr modifier la résolution et le taux de rafraîchissement
- 有道云笔记
- Integration of Android high-frequency interview questions (including reference answers)
- [no title] 2022 chlorination process examination content and free chlorination process examination questions
猜你喜欢

Preliminary cognition of C language pointer

Smart contract security audit company selection analysis and audit report resources download - domestic article

金仓KFS数据双向同步场景部署

Design and implementation of JSP logistics center storage information management system

GFS分布式文件系统(光是遇见已经很美好了)

data2vec! New milestone of unified mode

2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical

SSM based campus part-time platform for College Students
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem

redis 持久化原理
随机推荐
[fxcg] market analysis today
Youdao cloud notes
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
arthas watch 抓取入参的某个字段/属性
A outsourcing boy's mid-2022 summary
Interface in TS
Php+mysql registration landing page development complete code
一名外包仔的2022年中总结
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
Xrandr modifier la résolution et le taux de rafraîchissement
How to retrieve the password for opening word files
Writing skills of multi plate rotation strategy -- strategy writing learning materials
Kingbasees plug-in KDB of Jincang database_ exists_ expand
Mongodb slow query optimization analysis strategy
金仓数据库KingbaseES 插件kdb_date_function
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
MySQL field userid comma separated save by userid query
Basic use of continuous integration server Jenkins
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets