当前位置:网站首页>[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 !
边栏推荐
- Advanced learning of MySQL -- basics -- multi table query -- joint query
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- [daily problem insight] prefix and -- count the number of fertile pyramids in the farm
- Encryption algorithm - password security
- 37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
- Cross-entrpy Method
- 2022/2/10 summary
- Personal digestion of DDD
- Win10 startup error, press F9 to enter how to repair?
- Basic information of mujoco
猜你喜欢
2021 SASE integration strategic roadmap (I)
准备好在CI/CD中自动化持续部署了吗?
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
[software reverse automation] complete collection of reverse tools
Deep learning environment configuration jupyter notebook
Deep understanding of distributed cache design
VTK volume rendering program design of 3D scanned volume data
uniapp中redirectTo和navigateTo的区别
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
随机推荐
Markov decision process
.class文件的字节码结构
Mujoco second order simple pendulum modeling and control
【软件逆向-自动化】逆向工具大全
Mujoco Jacobi - inverse motion - sensor
三维扫描体数据的VTK体绘制程序设计
Mujoco finite state machine and trajectory tracking
Everyone is always talking about EQ, so what is EQ?
Business process testing based on functional testing
Leetcode(547)——省份数量
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Advantages and disadvantages of code cloning
Interface master v3.9, API low code development tool, build your interface service platform immediately
什么是时间
【JokerのZYNQ7020】AXI_EMC。
Rails 4 asset pipeline vendor asset images are not precompiled
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Hero League | King | cross the line of fire BGM AI score competition sharing
. Bytecode structure of class file
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析