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

边栏推荐
- 4 years of experience to interview test development, 10 minutes to end, ask too
- [fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
- Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
- 2022-02-14 (394. String decoding)
- Contents of welder (primary) examination and welder (primary) examination in 2022
- Human resource management system based on JSP
- Ffmpeg mix
- Library management system based on SSM
- Reptile exercise 03
- Preliminary cognition of C language pointer
猜你喜欢

Human resource management system based on JSP

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

redis 持久化原理

Joint search set: the number of points in connected blocks (the number of points in a set)

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

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

Redis persistence principle

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
![[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries](/img/56/386f0fd6553b8b9711e14c54705ae3.jpg)
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries

What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
随机推荐
Asp access teaching management system design finished product
多板块轮动策略编写技巧----策略编写学习教材
Kubernetes source code analysis (I)
Drf--- quick start 01
Bugku CTF daily question baby_ flag. txt
Redis persistence principle
FuncS sh file not found when using the benchmarksql tool to test kingbases
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
[fxcg] market analysis today
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
The latest activation free version of Omni toolbox
Summary of training competition (Lao Li's collection of questions)
金仓数据库KingbaseES 插件kdb_exists_expand
C language series - Section 3 - functions
redis 持久化原理
2022-02-14 (394. String decoding)
2.14 summary
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected