-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboats_to_save_people.py
44 lines (38 loc) · 1.54 KB
/
boats_to_save_people.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''
You are given an array people where people[i] is the weight of the ith 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 the minimum number of boats to carry every given person.
Maintain 2 pointers lo and hi set to 0 and n-1 respectively.
Sort the array people.
Now traverse till lo <= hi.
If people[lo] + people[hi] <= target. That means they can form a pair and can sit in the same boat.
If not then the people[hi] that is the person with the higher weight is the problem and must be given his own boat as we observed above.
'''
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
lo = 0
hi = len(people)-1
boats = 0
while lo <= hi:
if people[lo] + people[hi] <= limit:
lo += 1
hi -= 1
else:
hi -= 1
boats += 1
return boats
-----------------------------------------------------------------------------------------------------
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
count=0
i, j =0, len(people)-1
while i <= j :
if people[j]==limit or people[i]+people[j]>limit:
j-=1
count+=1
else:
i+=1
j-=1
count+=1
return count