Skip to content

Latest commit

 

History

History
32 lines (30 loc) · 923 Bytes

1204. Last Person to Fit in the Bus.md

File metadata and controls

32 lines (30 loc) · 923 Bytes

1204. Last Person to Fit in the Bus

Question Link - EN

问题链接 - 中文

3 Solutions

Code 1

with new as(
  select
  turn, person_name, weight, sum(weight) over(order by turn asc) totalweight
  from Queue
  order by turn
)
select person_name
from Queue q
where q.turn=(select max(turn) from new where totalweight<=1000)

Code 2

select q1.person_name from Queue q1 join Queue q2 on q1.turn>=q2.turn
group by q1.turn
having sum(q2.weight)<=1000
order by sum(q2.weight) desc
limit 1

Code 3

select p1.person_name from (
    select person_name, weight, turn, sum(weight) over(order by turn rows between unbounded preceding and current row) as "Total Weight" from Queue
) as p1 where p1.`Total Weight`<=1000 order by p1.`Total Weight` desc limit 1