Goodland Electricity

  • + 0 comments

    Java

    public static int pylons(int k, List<Integer> arr) {
            int ans = 0, n = arr.size();
            for(int i = 0; i < n; i++){
                
                // Power plant should be built on buildIdx
                int buildIdx = Math.min(i + (k - 1), n - 1);
                
                // Keep looking for a power plant
                while(buildIdx >= 0 && buildIdx < n && arr.get(buildIdx) == 0)
                    buildIdx--;
                    
                if(buildIdx >= 0 && buildIdx < n && arr.get(buildIdx) == 1 && Math.abs(buildIdx - i) < k){
                    ans++; // Build here
                    i = buildIdx + (k - 1);
                }
                else
                    return -1;
            }
            return ans;
        }