From fd7e4cbbec002699d75bdf79f87219394c335dc7 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Wed, 13 Jan 2021 17:34:39 +0100 Subject: [PATCH] Create 13.cc --- solved/LeetCode/Challenges/2020/January/13.cc | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/13.cc diff --git a/solved/LeetCode/Challenges/2020/January/13.cc b/solved/LeetCode/Challenges/2020/January/13.cc new file mode 100644 index 00000000..e7a7d951 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/13.cc @@ -0,0 +1,21 @@ +class Solution { +public: + int numRescueBoats(vector& people, int limit) { + sort(people.begin(), people.end()); + deque q; + for (auto it : people) q.push_back(it); + + int boats = 0; + + while (q.size() > 1) { + boats++; + int last = q.back(); + q.pop_back(); + if (last + q.front() <= limit) { + q.pop_front(); + } + } + + return boats + q.size(); + } +};