Codility/Lesson 15/Caterpillar method/CountDistinctSlices/Java

Taesun Lee·2020년 12월 9일
0

algorithm-study

목록 보기
5/10

Code

// 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 M, int[] A) {
        // write your code in Java SE 8
        int answer = 0;
        boolean checked[] = new boolean[M+1];
        int right = 0;

        for(int left = 0; left<A.length; left++) {
            while(right < A.length && !checked[A[right]]) {
                answer += (right - left + 1);
                checked[A[right]] = true;
                right++;
                
                if(answer >= 1000000000) {
                    return 1000000000;
                }
            }

            checked[A[left]] = false;

            if(answer >= 1000000000) {
                return 1000000000;
            }
        }

        return answer;
    }
}

Result URL

https://app.codility.com/demo/results/trainingQ6GPXQ-7JV/

profile
구름위 개발자

0개의 댓글