aboutsummaryrefslogtreecommitdiff
path: root/boats_save_people.py
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 /boats_save_people.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'boats_save_people.py')
-rw-r--r--boats_save_people.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/boats_save_people.py b/boats_save_people.py
new file mode 100644
index 0000000..92ae1d4
--- /dev/null
+++ b/boats_save_people.py
@@ -0,0 +1,32 @@
+# 881. Boats to Save People
+
+"""
+given an array 'people' where 'people[i]' is the weight of the i'th person,
+and an infinite number of boats where each boat can carry a maximum weight of
+'limit'. each boat carries at most two people at the same time, provided the
+sum of the weight of those people is at most 'limit'. return minimum number
+of boats to carry every person
+"""
+
+
+class Solution(object):
+ def numRescueBoats(self, people, limit):
+ """
+ :type people: List[int]
+ :type limit: int
+ :rtype: int
+ """
+ people.sort(reverse=True)
+ i, j = 0, len(people) - 1
+ while i <= j:
+ if people[i] + people[j] <= limit:
+ j -= 1
+ i += 1
+ return i
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numRescueBoats([1, 2], 3))
+ print(obj.numRescueBoats([3, 2, 2, 1], 3))
+ print(obj.numRescueBoats([3, 5, 3, 4], 5))