Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1642.furthest-building-you-can-reach.py #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Python/furthest-building-you-can-reach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'''
the condition was a person can use ladder or brick to climb a building but the sucessive building's height can be greater than or less than current buiiding.
no. of bricks and ladder given,ladder length can be anything i.e if next buliding in 2 units greater than and we have no bricks so we can use ladder.
so i used maxheap to know where should i use ladder so that it will not waste my resource and we can reach the farthest building.
'''
# import heapq
class Solution:
def furthestBuilding(self, heights: list[int], bricks: int, ladders: int) -> int:
heap = []
for i in range(len(heights)-1):
diff = heights[i+1]-heights[i]
print(diff)
if diff<0:
continue
print("hello",diff)
bricks-=diff
print("b",bricks)
heapq.heappush(heap,diff)
print(heap)
if bricks<0:
if ladders==0:
return i
ladders-=1
print("bricks",bricks,"ladder",ladders)
bricks+=heapq.heappop(heap)

return len(heights)-1