From eaf4e878bae0ba0c4cde2c81f58fbd83943fb522 Mon Sep 17 00:00:00 2001 From: Anirudh Shivarkar <40207836+Anirudh906@users.noreply.github.com> Date: Fri, 1 Oct 2021 15:57:27 +0530 Subject: [PATCH] fix #919 --- C++/Furthest_building_you_can_reach.cpp | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 C++/Furthest_building_you_can_reach.cpp diff --git a/C++/Furthest_building_you_can_reach.cpp b/C++/Furthest_building_you_can_reach.cpp new file mode 100644 index 0000000..34159b0 --- /dev/null +++ b/C++/Furthest_building_you_can_reach.cpp @@ -0,0 +1,45 @@ +/* +Problem link: https://leetcode.com/problems/furthest-building-you-can-reach/ +*/ + +class Solution +{ +public: + int furthestBuilding(vector &heights, int bricks, int ladders) + { + int n = heights.size(); + int i = 0; + priority_queue, greater> p; + while (i < n - 1 and p.size() < ladders) + { + int t = heights[i + 1] - heights[i]; + if (t > 0) + { + p.push(t); + } + i++; + } + while (i < n - 1) + { + int t = heights[i + 1] - heights[i]; + if (t > 0) + { + if (t > p.top() and !p.empty()) + { + bricks -= p.top(); + p.pop(); + p.push(t); + } + + else + { + bricks -= t; + } + } + if (bricks < 0) + return i; + i++; + } + return i; + } +}; \ No newline at end of file