当前位置:网站首页>[force buckle]41 Missing first positive number
[force buckle]41 Missing first positive number
2022-07-07 00:49:00 【wingaso】
Consistent with the topic AC Code
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
while nums[i] != i + 1 and nums[i] > 0 and nums[i] < n and nums[i] != nums[nums[i] - 1]:
#nums[i],nums[nums[i] - 1] = nums[nums[i] - 1],nums[i]
t1,t2 =nums[i],nums[nums[i] - 1]
nums[nums[i] - 1] = t1
nums[i] = t2
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1
Not in line with the title AC Code
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
D = {
}
for it in nums:D[it] = 1
i = 1
while i in D.keys(): i += 1
return i
analysis
Because the time complexity of this problem is O ( n ) O(n) O(n), And the value range of the element is the integer range , So the easiest way to think of is to use hash table —— With the help of hash table, mark every value that appears , The hash time of each value is O ( 1 ) O(1) O(1), The total complexity of hash is O ( n ) O(n) O(n). And then from 1 Start to find the first integer that does not appear in the hash table , The expected time complexity of the traversal process is O ( n / 2 ) O(n/2) O(n/2). So the total time complexity of this process is O ( n ) O(n) O(n), Meet the requirements of the topic . However , The problem also has strict requirements for the spatial complexity of the algorithm —— O ( 1 ) O(1) O(1) Time complexity of .
The spatial complexity of using hash tables is linear , Obviously does not meet the requirements of the topic .
For this question , Use hash table What a beautiful thing , But the space complexity of hash table is not up to standard , What are the optimization strategies ?
Here we need to use some characteristics of the data itself .
That is to build a special hash table with the help of the particularity of the data itself , achieve O ( 1 ) O(1) O(1) Complexity requirements .

As shown in the figure , We can put each number where it should be , The final result we use O ( n ) O(n) O(n) The complexity of completes the mapping of numeric values and array subscripts —— That is hash .
It should be noted that , The value of the number in the array may be negative 、 It may also be a large number .
For these numbers , We can ignore —— Because the array length is only n, So the first positive number missing cannot be greater than n + 1.
Are we using O ( n ) O(n) O(n) The time complexity of sorting an array ?
Yes .
According to conventional cognition , Sort an array , The most time complexity is O ( n l o g 2 n ) O(nlog_2n) O(nlog2n), Why can it be achieved here O ( n ) O(n) O(n) What about the order of ?
That's because we take advantage of the particularity of this array —— Or the particularity of sorting rules —— We only sort the data within a limited range of integers , And each number should not be repeated when sorting ( Otherwise, the number will be ignored ).
Topic link
Originality is not easy. , Thank you for your support !
边栏推荐
- Stm32f407 ------- DAC digital to analog conversion
- ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
- Matlab learning notes
- Advanced learning of MySQL -- basics -- multi table query -- subquery
- Threejs image deformation enlarge full screen animation JS special effect
- 深度学习之数据处理
- Attention SLAM:一種從人類注意中學習的視覺單目SLAM
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
- Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
- Leetcode (547) - number of provinces
猜你喜欢

Geo data mining (III) enrichment analysis of go and KEGG using David database

ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版

Attention slam: a visual monocular slam that learns from human attention

基於GO語言實現的X.509證書

37頁數字鄉村振興智慧農業整體規劃建設方案

Telerik UI 2022 R2 SP1 Retail-Not Crack

2022/2/10 summary

JWT signature does not match locally computed signature. JWT validity cannot be asserted and should

System activity monitor ISTAT menus 6.61 (1185) Chinese repair

【vulnhub】presidential1
随机推荐
Business process testing based on functional testing
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Quaternion attitude calculation of madgwick
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Equals() and hashcode()
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
stm32F407-------DAC数模转换
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
Distributed cache
【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
Levels - UE5中的暴雨效果
stm32F407-------SPI通信
Use mujoco to simulate Cassie robot
509 certificat basé sur Go
Advanced learning of MySQL -- basics -- multi table query -- self join
uniapp中redirectTo和navigateTo的区别
Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
Command line kills window process