The first thing that clicked my mind was window function select person_name from (select person_name, SUM(weight) over (order by turn) as running_weight from queue) temp where running_weight
@samuraychik1117 ай бұрын
good query bro !!
@Rahul-pr1zr3 жыл бұрын
Nice explanation! I was wondering though - isn't the GROUP BY qualifier needed as part of the SELECT statement or is it optional?
@ashwinikumar24927 ай бұрын
Interesting solution. I just wanted to know how would this solution fair in comparison to window functions when you have a larger data size and limit?
@summer_in_california Жыл бұрын
AWESOME SOLUTION. thanks for the video!!!
@YogeshKumar-zb5ms Жыл бұрын
how can i do it if the number of people in queue are 100s and it will tka multiple treips, in that case having X
@frederikmuller Жыл бұрын
interesting, I guess you would have to add a condition for the maximum amount of people that the elevator can fit in terms of space.
@frederikmuller Жыл бұрын
for this question you could add WHERE turn
@sohailkan17 Жыл бұрын
How's is the self join producing the cummulative sum result I am not getting Can someone explain ?
@frederikmuller Жыл бұрын
it basically just produces every possible join (1 row, 2 rows, 3 rows…) and then sums up the weight. we filter to weights
@leozeng669610 ай бұрын
The solution in the video fails in PostgreSQL. It doesn't allow me to select the person's name without group by it I need to change it up, so it could work in both MySQL and PostgreSQL SELECT a.person_name FROM queue a JOIN queue b ON a.turn >= b.turn GROUP BY 1 HAVING SUM(b.weight)
@frederikmuller10 ай бұрын
Thank you for providing your solution.
@عليسعد-ح3ك5ذ Жыл бұрын
I have a problem with the condition of the self join , I don't get it
@frederikmuller Жыл бұрын
it basically just produces every possible join (1 row, 2 rows, 3 rows…) and then sums up the weight. we filter to weights
@عليسعد-ح3ك5ذ Жыл бұрын
@@frederikmuller Thank you man ! I tried it on myself and got it , thank you again for replying
@UmaAndLak2 жыл бұрын
Thank you for the valuable video Frederick. "Link to the problem" in your comment, does not seem to work.
@frederikmuller11 ай бұрын
The link has actually changed as the question title has changed: leetcode.com/problems/last-person-to-fit-in-the-bus/
@aznfoever353 жыл бұрын
love these man! and great hair
@frederikmuller3 жыл бұрын
hehe thanks
@sandeep64603 жыл бұрын
Thanks from India
@SomeRandomName9999992 ай бұрын
Why sum(b.weight) instead of (a.weight)?
@sonalkulkarni62822 жыл бұрын
this is giving wrong answer. I tried to put "weight" in select statement along with person_name just to recheck,, it is fetching wrong value as there are 2 "Thomas Jefferson" names. Please check.
@samuraychik1117 ай бұрын
declare @weight int = 0 declare @turn int = 1 declare @currentweight int while @weight !=1000 begin select @currentweight = (select weight from elevator where turn = @turn) if @currentweight + @weight
@frederikmuller7 ай бұрын
interesting, thanks for sharing 🤔
@davidreilly52155 ай бұрын
Never thought of joining the tables. I used a window function with cte as ( SELECT person_id, person_name, SUM(weight) OVER(order by turn) as added_weight FROM Queue ) SELECT person_name FROM cte WHERE added_weight
@frederikmuller5 ай бұрын
clever
@Hsalz11 ай бұрын
Not "James Elephant" being 500 lbs. That was intentional.
@frederikmuller11 ай бұрын
I never noticed that 😮
@shubham9001003 жыл бұрын
I didnt know kurt hugo schneider was good at SQL
@riyashussain24732 жыл бұрын
why can't we just do HAVING SUM (b.weight)=1000 instead of less than or equal to.
@ramram991112 жыл бұрын
Let's consider currently the sum is 900 Now new person enters whose weight is 110 So the total will be 1110 In these cases we need to consider only till 900.
@tansmay233 жыл бұрын
Finally thank you!
@vishalbhardwaj9693 жыл бұрын
You are awesome
@sandeep64603 жыл бұрын
Nice man
@sandeep64603 жыл бұрын
Keep it up
@dineshshinde19997 ай бұрын
please do not refer his code. its a wrong code
@frederikmuller7 ай бұрын
the question has been updated since recording this video, check out the pinned comment for the updated solution.