Boats to Save People - Leetcode 881 - Python

preview_player
Показать описание


0:00 - Read the problem
1:15 - Drawing Explanation
4:49 - Coding Explanation

leetcode 881

#amazon #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

Inspired by your explanation:
people.sort()
l, r = 0, len(people)-1
res = 0

while l <= r:
if people[l] + people[r] <= limit:
l += 1
r -= 1
else:
r -= 1
res += 1

return res

akhma
Автор

LeetCode: faster than 3% of Python3 submissions
NeetCode: as you can see its prettty efficient xD

thanks for the great videos btw

addiegupta
Автор

Had not realized this was a competition problem. My very first one !!!! Lots and lots of thanks to you

vdyb
Автор

i used the 2 pointer approach also, but coded it a bit different.
while (left <= right) {
let weight = people[left] + people[right]
if (weight % limit === 0 || weight % limit === weight) {
count++;
left++;
right--;
} else {
count++;
right--;
}
}
return count;

johnsoto
Автор

There is no need to write l<=r inside if as it's already considered in the

arupmondal
Автор

The problem claims to be medium. Is there a more efficient solution, cuz this seemed pretty straight foreward

goblinkoma
Автор

If there wasn't the constraint of max 2 people on a boat, then this problem would be a bin-packing problem which is really hard

anonymoussloth
Автор

fun pb and crisp explanation as usual!!

poptart-br
Автор

as always clear explanation I just watched the drawing part, I coded it differently

asmahamdym
Автор

Bro this is the exact problem I’m working on for arrays. Not to sure but I think leetcode #941 and #11 is what I have to practice nexts

adrianford
Автор

I have a doubt, Why should I take highest & lowest everytime ?
If iam so much greedy, I'll choose highest & check for nearest number <= (limit-highest), & if it's possible, send them together. If it's not possible send only highest alone. And this logic goes on.

This can be achieved by binary search, but I'm unable to implement .
Help needed.

annubaba
Автор

what is the app used to do all these drawings and sketching?

redafakih
Автор

I think Kinda same
```
people.sort()
n = len(people)

c = 0
l = 0
r = n -1
while l <= r:
if people[l] + people[r] <= limit:
l += 1
r -= 1
c += 1
else:
r -= 1
c += 1


return c

```

stormshadow