aboutsummaryrefslogtreecommitdiff
path: root/last_stone_weight.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /last_stone_weight.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'last_stone_weight.cpp')
-rw-r--r--last_stone_weight.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/last_stone_weight.cpp b/last_stone_weight.cpp
new file mode 100644
index 0000000..c7422db
--- /dev/null
+++ b/last_stone_weight.cpp
@@ -0,0 +1,23 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int lastStoneWeight(vector<int> &stones) {
+ make_heap(stones.begin(), stones.end());
+ while (stones.size() > 1) {
+ int x = stones.front();
+ pop_heap(stones.begin(), stones.end());
+ stones.pop_back();
+ int y = stones.front();
+ pop_heap(stones.begin(), stones.end());
+ stones.pop_back();
+ if (x == y)
+ continue;
+ stones.push_back(x - y);
+ push_heap(stones.begin(), stones.end());
+ }
+ return stones.size() ? stones.front() : 0;
+ }
+};
+
+int main() {}