当前位置:网站首页>[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 !
边栏推荐
- Leetcode(547)——省份数量
- Interface master v3.9, API low code development tool, build your interface service platform immediately
- 浅谈测试开发怎么入门,如何提升?
- Stm32f407 ------- SPI communication
- 2022/2/11 summary
- The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
- Model-Free Prediction
- VTK volume rendering program design of 3D scanned volume data
- 【JokerのZYNQ7020】AXI_ EMC。
- 接口(接口相关含义,区别抽象类,接口回调)
猜你喜欢

37 page overall planning and construction plan for digital Village revitalization of smart agriculture

英雄联盟|王者|穿越火线 bgm AI配乐大赛分享

How engineers treat open source -- the heartfelt words of an old engineer

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

VTK volume rendering program design of 3D scanned volume data

【vulnhub】presidential1

New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML

The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation

Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing

System activity monitor ISTAT menus 6.61 (1185) Chinese repair
随机推荐
建立自己的网站(17)
代码克隆的优缺点
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
准备好在CI/CD中自动化持续部署了吗?
Advanced learning of MySQL -- basics -- basic operation of transactions
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
MySQL learning notes (mind map)
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Mujoco finite state machine and trajectory tracking
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
【JokerのZYNQ7020】AXI_EMC。
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
C9高校,博士生一作发Nature!
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
【vulnhub】presidential1
2022/2/12 summary
深度学习之线性代数