当前位置:网站首页>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

边栏推荐
- Basic use of continuous integration server Jenkins
- Pyqt control part (II)
- [nlp] - brief introduction to the latest work of spark neural network
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- 2022-02-14 (394. String decoding)
- Design and implementation of JSP logistics center storage information management system
- 使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
- RSRS index timing and large and small disc rotation
- The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
- [fxcg] market analysis today
猜你喜欢

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

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory

After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me

Basic use of continuous integration server Jenkins

2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units

解决bp中文乱码

data2vec! New milestone of unified mode

Database management tool, querious direct download
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem

The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
随机推荐
Prefix and (continuously updated)
有道云笔记
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
金仓数据库KingbaseES 插件kdb_database_link
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
Classes in TS
Kingbasees plug-in KDB of Jincang database_ database_ link
How to retrieve the password for opening word files
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
Contents of welder (primary) examination and welder (primary) examination in 2022
How to use kotlin to improve productivity: kotlin tips
FFMpeg example
Joint search set: the number of points in connected blocks (the number of points in a set)
Golang -- realize file transfer
Introduction of pointer variables in function parameters
Feature_selection
The latest activation free version of Omni toolbox
C Primer Plus Chapter 10, question 14 3 × 5 array
Mongodb slow query optimization analysis strategy
GFS分布式文件系统(光是遇见已经很美好了)