Ejemplo n.º 1
0
        public int FurthestBuilding(int[] heights, int bricks, int ladders)
        {
            var pq = new MyPriorityQueue();

            for (int i = 0; i < heights.Length - 1; i++)
            {
                int climb = heights[i + 1] - heights[i];
                if (climb <= 0)
                {
                    continue;
                }

                pq.add(climb);

                if (pq.size() > ladders)
                {
                    var first = pq.poll();
                    bricks -= first;
                }

                if (bricks < 0)
                {
                    return(i);
                }
            }

            return(heights.Length - 1);
        }