Codility – MaxCounters 문제 풀이 (난이도:중)

Codility – MaxCounters 문제 풀이 (난이도:중)

// 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 N, int[] A) {
        
        int maxCounter = N+1;
        int counters[] = new int[N];
        for(int i = 0 ; i < counters.length ; i++){
            counters[i] = 0;   
        }
        
        int nextMax = 0;
        int curMax = 0;
        for(int i = 0 ; i < A.length ; i++){
            int counterNumber = A[i];
            int counterIndex = counterNumber - 1;
            
            if(counterNumber < maxCounter){
                if(counters[counterIndex] <= curMax){
                    counters[counterIndex] = curMax;
                }
                counters[counterIndex]++;
                nextMax = Math.max(nextMax, counters[counterIndex]);
            }
            else{
                curMax = nextMax;
            }
        }
        
        for(int i = 0 ; i < counters.length ; i++){
            if(counters[i] < curMax){
                counters[i] = curMax;   
            }
        }
        
        return counters;
    }
}
8764 Total Views 1 Views Today