// 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
        
        if (A.length == 1) {
            return 0;
        }
        
        int start = 0;
        while (start < A.length) {
            if (A[start] == 0) {
                break;
            }
            start++;
        }
        
        if (start == A.length) {
            return 0;
        }
        
        int pCount = 0;
        int result = 0;
        for (int i = start; i < A.length; i++) {
            if (A[i] == 0) {
                pCount++;
            } else {
                result += pCount;
            }
            
            if (result > 1000000000) {
                result = -1;
                break;
            }
        }
            
        return result;
    }
}

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

[codility] lesson6 - Triangle  (0) 2020.08.15
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12

+ Recent posts