当前位置:网站首页>[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 !
边栏推荐
- 【vulnhub】presidential1
- Idea automatically imports and deletes package settings
- Mujoco produces analog video
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
- Service asynchronous communication
- Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
- Three methods to realize JS asynchronous loading
- Geo data mining (III) enrichment analysis of go and KEGG using David database
- 509 certificat basé sur Go
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
猜你喜欢
X.509 certificate based on go language
【软件逆向-自动化】逆向工具大全
uniapp中redirectTo和navigateTo的区别
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Service asynchronous communication
Mujoco finite state machine and trajectory tracking
Distributed cache
Interface master v3.9, API low code development tool, build your interface service platform immediately
48页数字政府智慧政务一网通办解决方案
Learn self 3D representation like ray tracing ego3rt
随机推荐
48 page digital government smart government all in one solution
Win10 startup error, press F9 to enter how to repair?
Rails 4 asset pipeline vendor asset images are not precompiled
Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
Mujoco finite state machine and trajectory tracking
Five different code similarity detection and the development trend of code similarity detection
准备好在CI/CD中自动化持续部署了吗?
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
Mujoco second order simple pendulum modeling and control
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
三维扫描体数据的VTK体绘制程序设计
Matlab learning notes
.class文件的字节码结构
Advantages and disadvantages of code cloning
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Service asynchronous communication
Leecode brushes questions to record interview questions 17.16 massagist
【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
Three methods to realize JS asynchronous loading
【软件逆向-自动化】逆向工具大全