leetcode - 41. First Missing Positive - Hard

descrition

Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

解析

注意题目的要求,时间复杂度: O(n)。如果没有时间复杂度的现在,我们可以对数组进行排序,并检查顺序数组中 positive 数的缺失情况即可,时间复杂度为O(nlog(n))。

在有时间复杂度限制的情况下,我们需要进一步分析题目的特点。

  • 此处还需要注意的是,positive 是指那些大于 0 的数
  • 要找到第一个缺失的正整数(所谓的第一个正整数,是指从 1 开始计数,第一个缺失的正整数)

基本原理:对于 k 个正整数(允许重复),第一个缺失的值必然在区间 [1,k+1] 内。可以想象成,将 k 个球放到 k+1 个箱子里,那么必然有至少有一个箱子是空的。

对于长度为 n 的整型数组 arry[],假设正整数的个数为 k 个,k<=n,那么缺失的值必然在区间 [1,k+1]内,我们可以将区间映射到 [0,k](当 k=n 时,区间为[0,n-1])。

(辅助理解:[1,k+1] 可以看成是从 1 开始顺序编号的箱子,直到 k+1,我们现在有 k 个正整数,正整数的数值表示起要放到几号桶,因为我们最多只有 k 个正整数,那么必然至少有一个桶时空的)

根据以上分析,对于任意正整数 1<=arry[i]<=n,我们可以从左边到右(从编号1开始到k)将其放到 arry[i]-1 的位置。满足 1<=arry[i]<=n 条件的数最多有 n 个。最后检查,如果存在 arry[i] != i+1 ,那么 i+1 就是缺失的第一个正整数,最极端的情况是 n 个数都满足 arry[i] == i+1,此时第一个缺失的正整数为 n + 1。(注意!!从左往右遍历)

具体实现如代码所示。注意,满足 1<=arry[i]<=n 条件的正整数有可能存在重复,因此需要检查即将要放置的目标位置是否已经满足条件,如果不满足条件才能放置。

code

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
// time-O(n), space-O(1)
int firstMissingPositive(vector<int>& nums){
int n = nums.size();
// put the element to the right place
// i must be increased from 0 to n, becasue we need to satisfy the lower number first
for(int i=0; i<n; i++){
while(nums[i] > 0 && nums[i] <= n && nums[nums[i]-1] != nums[i]){
// note: nums[nums[i]-1] != nums[i] indicate the place nums[nums[i]-1] hasn't satisfied
// so we can place the nums[i] to nums[nums[i]-1]
swap(nums[i], nums[nums[i]-1]);
}
} // find the first missing positive
for(int i=0; i<n; i++){
if(nums[i] != (i+1))
return i+1;
} return n + 1;
}
}; int main()
{
return 0;
}

[array] leetcode - 41. First Missing Positive - Hard的更多相关文章

  1. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  2. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  3. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  4. Java [Leetcode 41]First Missing Positive

    题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...

  5. [leetcode]41. First Missing Positive第一个未出现的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  6. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

    First Missing Positive  Given an unsorted integer array, find the first missing positive integer. Fo ...

  8. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

  9. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

随机推荐

  1. cocos2dx3.0导出自定义类到lua的方法详细步骤

    我写了一个用3.0的工具导出类到lua,自动生成代码的方法. 以前要导出c++类到lua,就得手动维护pkg文件,那简直就是噩梦,3.0以后就会感觉生活很轻松了. 下面我就在说下具体做法.1.安装必要 ...

  2. Spring框架——IOC依赖注入

    本来想把IOC和AOP一起介绍的,但是AOP内容太多了,所以就分开了,最后的结果就是这一篇只剩下一点点了.这不是第一次写关于IOC的文章了,之前写过Java反射,Java注解,也写过通过XML解析实现 ...

  3. 3255:十进制到六进制-poj

    3255:十进制到六进制 总时间限制:  1000ms 内存限制:  65536kB 描述 进制转换: 将十进制(不超过int类型表示的范围)的数转换为六进制的数. 输入 输入为第一行是组数n,后面n ...

  4. 乘积最大洛谷p1018

    题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...

  5. Spring Cloud Eureka服务Demo级搭建

    仅用于个人备忘,有错误之处还请文明指出,3Q 开发环境以及工具:Windows.JDK8.STS 1.新建SpringBoot工程作为Eureka Server                    ...

  6. 微信公众平台创建自定义菜单的PHP代码

    授人以鱼不如授人以渔.在方倍工作室上问了一下,创建自定义菜单的代码多少钱,一张口就一百,好吧,那我就给你们一人省一百块钱吧,你们说该如何谢谢我?事先说明一下啊,你的PHP版本要高于4.0.2才支持cU ...

  7. CountDownLatch的实现原理

    CountDownLatch是java并发包中辅助并发的工具类,目的是让并发运行的代码在某一个执行点阻塞,直到所有条件都满足,这里的条件就是调用countDown()方法,有点类似计数器的功能. 用法 ...

  8. 初识java这个小姑娘(二)

    妙解垃圾回收机制 周一,早高峰. 一段考验一个人耐力.智力.开车技术以及脾气的路. 我把车开进了一个没有红绿灯的丁字路口,然后就没有然后了. 来自三个方向的大车小车开始在不大的一块空间里开始互相斗智斗 ...

  9. MySql的隔离级别和锁的关系

    一.事务的4个基本特征  Atomic(原子性):  事务中包括的操作被看做一个逻辑单元.这个逻辑单元中的操作要  么所有成功.要么所有失败. Consistency(一致性):  仅仅有合法的数据能 ...

  10. C language 模拟 win的经典游戏——扫雷

    让我们在terminal下愉快的...扫雷 昨天跟奇葩霖聊起"雷区"这个敏感词汇,然后非常荣幸的... 应该轰炸不到我.. . 后来百无聊赖的去玩了把扫雷.然后发现我之前都是乱扫的 ...