Skip to content

Commit d7b47d3

Browse files
solves boats to save people (#881) in python
1 parent 1e6f3ee commit d7b47d3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

python/boats_to_save_people.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://leetcode.com/problems/boats-to-save-people/description/
2+
# T: O(nlogn) where n is the length of people
3+
# S: O(logn) for sorting
4+
5+
class Solution:
6+
def numRescueBoats(self, people: List[int], limit: int) -> int:
7+
people.sort()
8+
left, right, ans = 0, len(people) - 1, 0
9+
while left <= right:
10+
ans += 1
11+
if people[left] + people[right] <= limit:
12+
left+=1
13+
right-=1
14+
return ans

0 commit comments

Comments
 (0)