ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • MissingInteger
    Study/알고리즘 2018. 1. 12. 10:24

    퍼포먼스... 안습




    public static int solution(int[] A){
    
        int checkNum = 1;
        while(checkNum < A.length + 1){
            boolean isIn = false;
            for(int i = 0; i < A.length ; i++){
                if(checkNum == A[i]){
                    isIn = true;
                }
            }
            if(isIn && checkNum == A.length){
                return checkNum + 1;
            }
            else if(isIn){
                checkNum++;
            }
            else{
                return checkNum;
            }
        }
        return 1;
    }
    


    두번째 도전


    두번째 만에 100%라니!! 대에박~ 기분 좋고!

    퍼포먼스는 하나의 배열에 루프를 돌리는거 보다.. 

    배열 하나 카피개념으로 만들어서 그 안을 체크하는게 더 좋게 나오는 것 같다. 


    public static int solution(int[] A){
    
        boolean boolStack[] = new boolean[A.length + 1];
        boolean isAllTrue = false;
    
        for(int i = 0; i < A.length ; i++){
            int val = A[i];
            if(val < boolStack.length && val > 0){
                boolStack[val] = true;
            }
        }
    
        for(int j = 1 ; j < boolStack.length; j++){
            if(!boolStack[j]){
                return j;
            }
    
            if(j == boolStack.length -1){
                isAllTrue = true;
            }
        }
    
        if(isAllTrue){
            return A.length + 1;
        }
    
        return 1;
    }
    

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

    MinAvgTwoSlice  (0) 2018.01.15
    GenomicRangeQuery  (0) 2018.01.15
    PassingCars  (0) 2018.01.12
    CountDiv  (0) 2018.01.12
    MaxCounters  (0) 2018.01.12
    FrogRiverOne  (0) 2018.01.12
    PermCheck  (0) 2018.01.11
    PermMissingElem  (0) 2018.01.11
    FrogJmp  (0) 2018.01.11
    CyclicRotation  (0) 2018.01.11

    댓글

COPYRIGHT 2010 EpoNg. ALL RIGHTS RESERVED.