SunRise Counter

public class SunriseViewCounter {

    public static int countBuildings(int[] h, int n) {
        if (n == 0) return 0;
        int count = 1;
        int maxSoFar = h[0];
        for (int i = 1; i < n; i++) {
            if (h[i] > maxSoFar) {
                count++;
                maxSoFar = h[i];
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] heights1 = {17, 14, 18, 12, 19};
        int[] heights2 = {12, 13, 14, 15};
        int[] heights3 = {20, 18, 16, 14};
        int[] heights4 = {};

        System.out.println("Output 1: " + countBuildings(heights1, heights1.length));
        System.out.println("Output 2: " + countBuildings(heights2, heights2.length));
        System.out.println("Output 3: " + countBuildings(heights3, heights3.length));
        System.out.println("Output 4: " + countBuildings(heights4, heights4.length));
    }
}

 

Scroll to Top