// 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 11
        int cnt = 0;
        int cntSum = 0;
        int total = 0;

        for (int i = A.length - 1; i >= 0; i--) {

            if (A[i] == 1) {
                cnt++;
            } else {
                cntSum += cnt; // 2 // 3
                total += cntSum; // 2 // 5

                if (total > 1000000000) {
                    total = -1;
                    break;
                }

                cnt = 0;
            }
        }

        return total;
    }
}

코딜리티 PassingCars 문제 풀이 Java 소스 코드

+ Recent posts