当前位置:网站首页>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
边栏推荐
- [set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
- Arthas watch grabs a field / attribute of the input parameter
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Pyqt control part (II)
- arthas watch 抓取入参的某个字段/属性
- The latest activation free version of Omni toolbox
- Redis persistence principle
- 一名外包仔的2022年中总结
- Xrandr modifier la résolution et le taux de rafraîchissement
猜你喜欢
stm32逆向入门
GFS distributed file system (it's nice to meet it alone)
P35-P41 fourth_ context
X-ray normal based contour rendering
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
Joint search set: the number of points in connected blocks (the number of points in a set)
Some information about the developer environment in Chengdu
Joint set search: merge intervals and ask whether two numbers are in the same set
Which code editor is easy to use? Code editing software recommendation
一名外包仔的2022年中总结
随机推荐
金仓数据库KingbaseES 插件kdb_date_function
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
一名外包仔的2022年中总结
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
Prefix and (continuously updated)
Pdf editing tool movavi pdfchef 2022 direct download
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
MC Layer Target
Internationalization and localization, dark mode and dark mode in compose
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
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)
2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
[fxcg] market analysis today
Basic types of data in TS
Why should programmers learn microservice architecture if they want to enter a large factory?
怎么用Kotlin去提高生产力:Kotlin Tips
[set theory] set concept and relationship (set family | set family examples | multiple sets)
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found