aboutsummaryrefslogtreecommitdiff
path: root/other/furthest_building_reach.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/furthest_building_reach.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/furthest_building_reach.cpp')
-rw-r--r--other/furthest_building_reach.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/other/furthest_building_reach.cpp b/other/furthest_building_reach.cpp
new file mode 100644
index 0000000..6baff33
--- /dev/null
+++ b/other/furthest_building_reach.cpp
@@ -0,0 +1,32 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int furthestBuilding(vector<int> &heights, int bricks, int ladders) {
+ int n = heights.size() - 1;
+ priority_queue<int> pq;
+ for (int i = 0; i < n; i++) {
+ int diff = heights[i + 1] - heights[i];
+ if (diff > 0) {
+ if (ladders)
+ pq.push(-diff), ladders--;
+ else if (!pq.empty() && diff > -pq.top()) {
+ pq.push(-diff);
+ bricks += pq.top();
+ pq.pop();
+ } else
+ bricks -= diff;
+ if (bricks < 0)
+ return i;
+ }
+ }
+ return n;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> heights = {4, 2, 7, 6, 9, 14, 12};
+ int bricks = 5, ladders = 1;
+ cout << obj.furthestBuilding(heights, bricks, ladders);
+}