Advertisement
DeepRest

Furthest Building You Can Reach

Jun 21st, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. class Solution:
  2.     def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int:
  3.         pq = []
  4.         n = len(heights)
  5.         remSum = 0
  6.        
  7.         ans = n-1
  8.         for i in range(1, n):  
  9.             x = max(heights[i] - heights[i-1], 0)
  10.            
  11.             if x == 0:
  12.                 continue
  13.                
  14.             heappush(pq, x)    
  15.             if len(pq) > ladders:
  16.                 remSum += heappop(pq)
  17.    
  18.             if remSum > bricks:
  19.                 ans = i-1
  20.                 break
  21.        
  22.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement