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. ssh密钥登录

    一.生成密钥对(两种方式)并配置 方式1:使用ssh-keygen(1)生成并配置 (1)生成密钥对 [root@iZwz9catu2mrq92b07d1d0Z ~]# ssh-keygen -t r ...

  2. Libevent 事件管理和添加事件

    /**   我们先来看一下事件的创建*/struct event * event_new(struct event_base *base, evutil_socket_t fd, short even ...

  3. Ceph luminous 安装配置

    Ceph luminous 安装配置 #环境centos7 , Ceph V12 openstack pike 与 ceph 集成 http://www.cnblogs.com/elvi/p/7897 ...

  4. c# textbox的滚动条总是指向最底端

    当我第一次添加滚动条时候,我发现滚动条总是跑向上方,经过研究 解决方案如下: this.textBox1.Focus(); 获取焦点 this.textBox1.Select(this.textBox ...

  5. Python笔记·第六章——字典 (dict) 的增删改查及其他方法

    字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可 ...

  6. Java设计模式之职责链设计模式

    1.什么是-职责链设计模式 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求 ...

  7. 安装phpnow服务[Apache_pn]提示失败的解决方法

    win 7/win 8/Win10 phpnow提示"安装服务[Apache_pn]失败"错误解决办法汇总 常常在安装phpnow的时候,提示"安装服务 [ Apache_pn ] 失败&q ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. Bootstrap全局CSS样式之排版

    Bootstrap能全然友好的支持html5的文本元素,这里不再赘述,详细可參考我还有一篇文章<html的文本元素总结>,这里主要针对Bootstrap封装好的CSS文本样式做一下汇总. ...

  10. Oracle之PLSQL

    PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用.PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)它是Ora ...