LeetCode 1204: Last Person to Fit in the Elevator/Bus [SQL]

  Рет қаралды 5,226

Frederik Müller

Frederik Müller

Күн бұрын

Пікірлер
@debashish_dutta
@debashish_dutta 2 жыл бұрын
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
@samuraychik111
@samuraychik111 7 ай бұрын
good query bro !!
@Rahul-pr1zr
@Rahul-pr1zr 3 жыл бұрын
Nice explanation! I was wondering though - isn't the GROUP BY qualifier needed as part of the SELECT statement or is it optional?
@ashwinikumar2492
@ashwinikumar2492 7 ай бұрын
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
@summer_in_california Жыл бұрын
AWESOME SOLUTION. thanks for the video!!!
@YogeshKumar-zb5ms
@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
@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
@frederikmuller Жыл бұрын
for this question you could add WHERE turn
@sohailkan17
@sohailkan17 Жыл бұрын
How's is the self join producing the cummulative sum result I am not getting Can someone explain ?
@frederikmuller
@frederikmuller Жыл бұрын
it basically just produces every possible join (1 row, 2 rows, 3 rows…) and then sums up the weight. we filter to weights
@leozeng6696
@leozeng6696 10 ай бұрын
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)
@frederikmuller
@frederikmuller 10 ай бұрын
Thank you for providing your solution.
@عليسعد-ح3ك5ذ
@عليسعد-ح3ك5ذ Жыл бұрын
I have a problem with the condition of the self join , I don't get it
@frederikmuller
@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ذ
@عليسعد-ح3ك5ذ Жыл бұрын
@@frederikmuller Thank you man ! I tried it on myself and got it , thank you again for replying
@UmaAndLak
@UmaAndLak 2 жыл бұрын
Thank you for the valuable video Frederick. "Link to the problem" in your comment, does not seem to work.
@frederikmuller
@frederikmuller 11 ай бұрын
The link has actually changed as the question title has changed: leetcode.com/problems/last-person-to-fit-in-the-bus/
@aznfoever35
@aznfoever35 3 жыл бұрын
love these man! and great hair
@frederikmuller
@frederikmuller 3 жыл бұрын
hehe thanks
@sandeep6460
@sandeep6460 3 жыл бұрын
Thanks from India
@SomeRandomName999999
@SomeRandomName999999 2 ай бұрын
Why sum(b.weight) instead of (a.weight)?
@sonalkulkarni6282
@sonalkulkarni6282 2 жыл бұрын
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.
@samuraychik111
@samuraychik111 7 ай бұрын
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
@frederikmuller
@frederikmuller 7 ай бұрын
interesting, thanks for sharing 🤔
@davidreilly5215
@davidreilly5215 5 ай бұрын
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
@frederikmuller
@frederikmuller 5 ай бұрын
clever
@Hsalz
@Hsalz 11 ай бұрын
Not "James Elephant" being 500 lbs. That was intentional.
@frederikmuller
@frederikmuller 11 ай бұрын
I never noticed that 😮
@shubham900100
@shubham900100 3 жыл бұрын
I didnt know kurt hugo schneider was good at SQL
@riyashussain2473
@riyashussain2473 2 жыл бұрын
why can't we just do HAVING SUM (b.weight)=1000 instead of less than or equal to.
@ramram99111
@ramram99111 2 жыл бұрын
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.
@tansmay23
@tansmay23 3 жыл бұрын
Finally thank you!
@vishalbhardwaj969
@vishalbhardwaj969 3 жыл бұрын
You are awesome
@sandeep6460
@sandeep6460 3 жыл бұрын
Nice man
@sandeep6460
@sandeep6460 3 жыл бұрын
Keep it up
@dineshshinde1999
@dineshshinde1999 7 ай бұрын
please do not refer his code. its a wrong code
@frederikmuller
@frederikmuller 7 ай бұрын
the question has been updated since recording this video, check out the pinned comment for the updated solution.
@SagarKumar-db2xy
@SagarKumar-db2xy 3 жыл бұрын
Great
LeetCode 1907: Count Salary Categories [SQL]
9:42
Frederik Müller
Рет қаралды 725
LeetCode Medium 1164 Amazon Interview SQL Question with Detailed Explanation
9:50
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
LeetCode 1454: Active Users [SQL]
14:01
Frederik Müller
Рет қаралды 6 М.
LeetCode 1270: All People Report to the Given Manager [SQL]
10:09
Frederik Müller
Рет қаралды 4,6 М.
LeetCode 1661: Average Time of Process per Machine [SQL]
8:16
Frederik Müller
Рет қаралды 10 М.
LeetCode 550: Game Play Analysis IV [SQL]
8:31
Frederik Müller
Рет қаралды 10 М.
MLV Prasad - LeetCode SQL [ EASY ] | 0197 | "Rising Temperature" |
5:32
Data Science University - MLV Prasad
Рет қаралды 3,8 М.
Monthly Transactions I LeetCode 1193
18:39
Moshe Shamouilian
Рет қаралды 4
LeetCode 1341: Movie Rating [SQL]
18:35
Frederik Müller
Рет қаралды 169
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН