룬아님의 취중코딩

Codility 10번 MissingInteger 본문

개발/알고리즘

Codility 10번 MissingInteger

룬아님 2019. 8. 11. 20:09

MissingInteger

 

 

This is a demo task.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−1,000,000..1,000,000].

 

 

// you can also use imports, for example:
import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int[] A) {
        Arrays.sort(A);

        int ans = 1;
        
        if(A[A.length - 1] <= 0){
            return 1;
        }
        for(int i=0; i < A.length; i++){
            if(A[i] > 0 && A[i] == ans){
                ans++;
            }
        }
        
        return ans;
        // write your code in Java SE 8
    }
}
반응형

'개발 > 알고리즘' 카테고리의 다른 글

Codility 9번 MaxCounters  (0) 2019.08.12
Codility 7번 PermCheck  (0) 2019.08.12
Codility 6번 TapeEquilibrium  (0) 2019.08.05
Codility 5번 PermMissingElem  (0) 2019.08.05
Codility 8번 FrogRiverOne  (0) 2019.08.05
Comments