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

边栏推荐
- Which code editor is easy to use? Code editing software recommendation
- Bugku CTF daily question baby_ flag. txt
- [nlp] - brief introduction to the latest work of spark neural network
- [fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
- 2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
- Ffmpeg mix
- What's wrong with SD card data damage? How to recover SD card data damage
- Dive into deep learning - 2.1 data operation & Exercise
- 使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
- Crazy scientist
猜你喜欢

4 years of experience to interview test development, 10 minutes to end, ask too

Database management tool, querious direct download

2022 P cylinder filling test content and P cylinder filling simulation test questions

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination

How to choose cross-border e-commerce multi merchant system

Design and implementation of JSP logistics center storage information management system
![[free completion] development of course guidance platform (source code +lunwen)](/img/14/7c1c822bda050a805fa7fc25b802a4.jpg)
[free completion] development of course guidance platform (source code +lunwen)

Web - Information Collection

Solve BP Chinese garbled code

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
随机推荐
商城系统搭建完成后需要设置哪些功能
Priv app permission exception
[Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
4 years of experience to interview test development, 10 minutes to end, ask too
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
金仓数据库KingbaseES 插件kdb_exists_expand
Joint search set: the number of points in connected blocks (the number of points in a set)
[dynamic programming] subsequence problem
Xrandr modifier la résolution et le taux de rafraîchissement
Writing skills of multi plate rotation strategy -- strategy writing learning materials
Function introduction of member points mall system
Classes in TS
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
MongoDB 慢查询语句优化分析策略
Daily question - ugly number
Why should programmers learn microservice architecture if they want to enter a large factory?
C primre plus Chapter 10 question 6 inverted array
C Primer Plus Chapter 10, question 14 3 × 5 array
Drf--- quick start 01