Lintcode Medium title: First Missing Positive missing a positive integer

Source: Internet
Author: User
Tags lintcode

Topic

the first positive integer missing

Give an unordered array of integers to find the smallest positive integers that do not appear.

Sample Example

If given [1,2,0] , return 3 if given [3,4,-1,1] , return2

challenges

Only the time-complexity O (n) algorithm is allowed, and only constant-level space can be used.

Solving

The feeling seems to be very complicated, passing rate of 21% is also relatively low.

Found a bad way to do it.

Steps:

1. First find the minimum value of an integer in the array, min

2. If min>1 is clearly the smallest positive integer in the array is 1

3. Here are the cases where min = = 1 is the minimum value

For this case, it is only necessary to determine whether the smallest I, min + i is in the positive integer part of the array, where a ArrayList is required to store the number of positive integer parts, so that it is convenient to determine whether Min+i is in ArrayList. The min+i of the smallest i is the answer.

Here time complexity and space complexity are both O (N)

 Public classSolution {/**         * @parama:an array of integers *@return: An integer*/     Public intFirstmissingpositive (int[] A) {//Write your code here        if(A.length ==0)            return1; //if (a.length ==1) {//if (a[0]<= 0)//return 1; //return a[0]+1; // }        //1. Find the minimum value for positive numbers//2. Minimum value >1 return 1//3. Minimum value <0 return 1//4. The minimum value is 1, the minimum value up plus 1 does not exist is the answer        intMin =Integer.max_value; intMinint =Integer.max_value; ArrayList<Integer> list =NewArraylist<integer>();  for(inti=0;i< a.length;i++){            if(a[i]>0) {List.add (a[i]); if(A[i] <Min) Min=A[i]; }         }        if(min>1 )            return1; inti = 1; //minimum value equals 1         while(i<=a.length) {            if(!list.contains (Min +i)) {Minint= Min +i;  Break; } I= i + 1; }        returnminint; }}
Java Code

Total time: 2210 Ms

This and looking for the missing number seems very similar, although the rewritten program, understand, but leetcode test sample, resulting in unable to pass

Example: [about] This will get into a dead loop.

The procedure is as follows

 Public classSolution {/**         * @parama:an array of integers *@return: An integer*/     Public intFirstmissingpositive (int[] A) {//Write your code here        if(A.length = = 0)            return1; intn =a.length;  for(intI =0;i< N; i++){             while(A[i]! = i + 1){                if(A[i] <= 0 | | A[i] >N) Break; intTMP =A[i]; A[i]= A[tmp-1]; A[tmp-1] =tmp; }        }         for(intI =0;i< N; i++)            if(A[i]! = i+ 1)                returnI+1; returnn + 1; }}
Java Code

classSolution:#@param A, a list of integers    #@return An integer    deffirstmissingpositive (Self, A):#Write your code here        ifA = =None:return1N=Len (A) Nums=a[:] forIinchrange (n): whileA[i]! = i+1: Nums=a[:]ifA[i]<=0ora[i]>=N: Breaktmp=A[i] A[i]= A[tmp-1] A[tmp-1] =tmp#print ' Before: ', Nums                #print ' later: ', A         forIinchrange (n):ifa[i]!= I +1:                returnI+1returnN +1
Python Code

Lintcode Medium title: First Missing Positive missing a positive integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.