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. LKD: Chapter 6 Kernel Data Structures

    这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees. Linked Lists:(<linux/list.h>) 在lin ...

  2. [读书笔记]javascript语言精粹'

    人比较笨,以前只做项目,案例,然而一些javascript的很多理论不知道该怎么描述,所以最近开启一波读书之旅: 标识符 1.定义 标识符以字母开头,可能后面跟上一个或多个字母.数字或者下划线. 2. ...

  3. 流式处理的新贵 Kafka Stream - Kafka设计解析(七)

    原创文章,转载请务必将下面这段话置于文章开头处. 本文转发自技术世界,原文链接 http://www.jasongj.com/kafka/kafka_stream/ Kafka Stream背景 Ka ...

  4. 关于tolua的使用

    一.首先在引擎的跟目录下找到cocos2d-x自带的工具tolua++ 二.使用tolua++生成自定义类的声明 打开tool文件夹中的readme文件如下: 1. Generating the lu ...

  5. TFboy养成记 MNIST Classification (主要是如何计算accuracy)

    参考:莫烦. 主要是运用的MLP.另外这里用到的是批训练: 这个代码很简单,跟上次的基本没有什么区别. 这里的lossfunction用到的是是交叉熵cross_entropy.可能网上很多形式跟这里 ...

  6. Pyhton编程(六)之基本数据类型-集合(补充)

    集合(set) 集合其实就是一个无序的,自动去重的数据集合,它主要的作用是用来去重和进行关系测试,集合的定义方法如下: name=set("czp") /name=set({1,2 ...

  7. 找到链表的倒数第K位

    #include<iostream> using namespace std; class node{ public: node():value(),next(NULL){} ~node( ...

  8. Windows下根据端口号查找进程并关闭

    经常用到,但是总记不住命令,备忘一下…… netstat -aon|findstr "8080"  找到进程号 tasklist|findstr "7200"  ...

  9. CCF-201409-1-相邻数对

    问题描述 试题编号: 201409-1 试题名称: 相邻数对 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. ...

  10. linux上kafka模拟客户端发送、接受消息

    producer   消息的生成者,即发布消息 consumer   消息的消费者,即订阅消息 broker     Kafka以集群的方式运行,可以由一个或多个服务组成,服务即broker zook ...