From 79097f742260a7c05fbe98cb96bf4aa8d978afa2 Mon Sep 17 00:00:00 2001 From: sumanth-botlagunta Date: Mon, 3 Apr 2023 17:30:09 +0530 Subject: [PATCH] 881: Boats to Save People --- python/boats_to_save_people.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 python/boats_to_save_people.py diff --git a/python/boats_to_save_people.py b/python/boats_to_save_people.py new file mode 100644 index 0000000..5d795e8 --- /dev/null +++ b/python/boats_to_save_people.py @@ -0,0 +1,14 @@ +# https://leetcode.com/problems/boats-to-save-people/description/ +# T: O(nlogn) where n is the length of people +# S: O(logn) for sorting + +class Solution: + def numRescueBoats(self, people: List[int], limit: int) -> int: + people.sort() + left, right, ans = 0, len(people) - 1, 0 + while left <= right: + ans += 1 + if people[left] + people[right] <= limit: + left+=1 + right-=1 + return ans