알고리즘 풀이/Codility

codility - Time Complexity (PermMissingElem)

배게 2019. 12. 10. 00:53
728x90

시간복잡도 때문에 2번 틀림, 정렬이나 그런 것 하지말고 O(n)으로 한번에 끝내야함


1
2
3
4
5
6
7
8
9
10
11
12
13
    public int solution(int[] A) {
        // write your code in Java SE 8
        int leng = A.length+1;
        int sum=0;
        if(leng%2==0) sum=(leng+1)*(leng/2);
        if(leng%2==1) sum=((leng+1)/2)*(leng);
        
        for(int a : A){
            sum-=a;
        }
        
        return sum;
    }
cs