From a9dbd32bb13764399ee299cd17ef327f514d5673 Mon Sep 17 00:00:00 2001 From: Manoj Nayak <106598910+manojvn2612@users.noreply.github.com> Date: Fri, 3 May 2024 13:01:39 +0530 Subject: [PATCH] Create furthest-building-you-can-reach.py --- Python/furthest-building-you-can-reach.py | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Python/furthest-building-you-can-reach.py diff --git a/Python/furthest-building-you-can-reach.py b/Python/furthest-building-you-can-reach.py new file mode 100644 index 00000000..ddbc9ea5 --- /dev/null +++ b/Python/furthest-building-you-can-reach.py @@ -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