Koko Eating Bananas - Leetcode 875 - Binary Search (Python)

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


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

koko is overthinking this, just eat and be happy and leave us out of it

ramineye
Автор

def koko(eating_capacity):
if eating_capacity <= 1000:
return "Koko is a cute little monkey"
elif eating_capacity > 1000:
return "Koko has transcended to MONSTER status"
else:
return "Koko is... in a mysterious state”

ParthPatel-xbcn
Автор

I’ve said it before and I’ll say it again - you are such a gifted teacher!

hyperboliq
Автор

class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
l, r = 1, max(piles)
res = r

while l <= r:
k = (l + r) // 2

totalTime = 0
for p in piles:
totalTime += math.ceil(float(p) / k)
if totalTime <= h:
res = k
r = k - 1
else:
l = k + 1
return res

DennisSimplifies
Автор

You taught this so well. I was computing given h how many bananas koko(i also love banana)could eat. So far I always have this confusion on what should i calculate to shift the right/left. Today I think I got the drift.

chandrikasaha
Автор

O my god! this is amazing . thank you sir. I have watch 2 videos before even tried myself . After watching this I successfully understand it.

BananaButcher
Автор

Hey Greg. I found that l <= r works if you have if m==r and r==l: return r within the while loop. That when when all 3 values equal because it found a match it returns r

Johnson_Amah
Автор

Hello, I’ve recently started learning binary search and have encountered several challenges. I often struggle with deciding whether to use while l <= r or while l < r. Also, I’ve noticed that sometimes you use three branches (if, elif, else), and other times just two (if and else). Do you decide on the loop condition and the number of branches after completing your code? Here is my implementation; I would appreciate your feedback:

res = r
while l <= r:
m = (l + r) // 2
time = 0
for p in piles:
time += math.ceil(p / m)
if h >= time:
res = m
r = m - 1
else:
l = m + 1
return res

zhihangcheng
Автор

this problem was hard (as u said its surprising we can use binary in other ways than just normal search)

crtlallaw
Автор

I would have liked and illustrated explanation to the tech you used at the end, also wouldnt checking if a k works then checking k-1 doesnt work then that k is my answer

younis
Автор

Guru, you are greater than chatgpt🤗🤗. May I meet you one day.

KumarKalyan-qdjx
Автор

Hey Greg, for some reason this solution does not work anymore. I thought it was just my code but pasting your code from github still doesn't pass the testcases 🤔

ChespinisEpic
Автор

Great video! But I believe that the slowest speed could be optimized to math.ceil(sum(piles) / h), not 1

zhihangcheng
Автор

This is first bad version but instead you make the function and you're looking for the first good version instead.

Thomas-vmjy
Автор

what does the function k_works return??
what is meant by if k_works(k) ??

lakshyamathur
Автор

while solving this problem I am craving bananas 🤣

mumuuheibjf
Автор

My question is why is binary search working even when the arrray is unsorted

DivineEdoka
welcome to shbcf.ru