package leetcode;

/**
 * https://leetcode.com/problems/furthest-building-you-can-reach/
 */
public class Problem1642 {
    public int furthestBuilding(int[] heights, int bricks, int ladders) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1642 prob = new Problem1642();
        System.out.println(prob.furthestBuilding(new int[]{4,2,7,6,9,14,12}, 5, 1)); // 4
        System.out.println(prob.furthestBuilding(new int[]{4,12,2,7,3,18,20,3,19}, 10, 2)); // 7
        System.out.println(prob.furthestBuilding(new int[]{14,3,19,3}, 17, 0)); // 3
    }
}

@@ -12,16 +12,49 @@
 tcode;%0A%0A
+import java.util.PriorityQueue;%0A%0A
 /**%0A * h
@@ -232,403 +232,578 @@
     
-// TODO%0A        return 0;
+PriorityQueue%3CInteger%3E pq = new PriorityQueue%3C%3E((a, b) -%3E Integer.compare(b, a));%0A        for (int i = 1; i %3C heights.length; i++) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1642 prob = new Problem1642();%0A        System.out.println(prob.furthestBuilding(new int%5B%5D%7B4,2,7,6,9,14,12%7D, 5, 1)); // 4%0A        System.out.println(prob.furthestBuilding(new int%5B%5D%7B4,12,2,7,3,18,20,3,19%7D, 10, 2)); // 7%0A        System.out.println(prob.furthestBuilding(new int%5B%5D%7B14,3,19,3%7D, 17, 0)); // 3
+    int diff = heights%5Bi%5D - heights%5Bi - 1%5D;%0A            if (diff %3E 0) %7B%0A                pq.add(diff);%0A                bricks -= diff;%0A                if (bricks %3C 0) %7B%0A                    ladders--;%0A                    bricks += pq.poll();%0A                    if (bricks %3C 0 %7C%7C ladders %3C 0) %7B%0A                        return i - 1;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return heights.length - 1;
 %0A   

Solve problem 1642