Codility – TapeEquilibrium 문제 풀이 (난이도 : 하)

Codility –  TapeEquilibrium 문제 풀이 (난이도 : 하)

// 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) {
        // write your code in Java SE 8
        
        int front = 0;
        int back = 0;
        for(int i = 0 ; i < A.length ; i++){
            back += A[i];   
        }
        
        int minDiff = Integer.MAX_VALUE;
        int p = 1;
        for(int i = 1 ; i < A.length ; i++){
            front += A[i-1];
            back -= A[i-1];
            p = i;
            minDiff = Math.min(minDiff, Math.abs(front - back));
        }
        
        return minDiff;
        
    }
}

Codility – OddOccurrencesInArray 문제 풀이 (난이도 : 하)

Codility – OddOccurrencesInArray 문제 풀이 (난이도 : 하)

// you can also use imports, for example:
// import java.util.*;
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) {
        // write your code in Java SE 8
        HashMap<Integer, Boolean> pairedMap = new HashMap();

        for(int i = 0 ; i < A.length ; i++){
            int occurredNumber = A[i];
            Boolean paired = pairedMap.get(occurredNumber);
            if(paired == null || paired == true){
                pairedMap.put(occurredNumber, false);
            }
            else{
                pairedMap.put(occurredNumber, true);
            }
        }

        Iterator iterator = pairedMap.keySet().iterator();
        int minValue = Integer.MAX_VALUE;
        while(iterator.hasNext()){
            int key = iterator.next();
            Boolean paired = pairedMap.get(key);
            if(paired == false){
                minValue = Math.min(minValue, key);
            }
        }

        return minValue;
    }
}

Codility – BinaryGap 문제 풀이 (난이도 : 하)

Codility – BinaryGap 문제 풀이 (난이도 : 하)

사이트에서 문제 복제 및 배포(publish)는 허가하지 않는다니 그냥 풀이만……

class Solution {
    
    public int solution(int N) {
        int mode = 0;
        
        //remove 0 gaps without 1
        do{
            mode = N%2;
            N = N/2;
        }while(mode == 0);
        
        //count 0 gaps and get max 0 gap count
        int maxGapCount = 0;
        int gapCount = 0;
        while(N != 0){
            mode = N%2;
            N = N/2;
            if(mode == 1){
                maxGapCount = Math.max(maxGapCount,gapCount);
                gapCount = 0;
            }
            else{
                gapCount++;
            }
        }
        
        return maxGapCount;
    }
}