Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1204. Last Person to Fit in the Bus

์ธ” 2023. 9. 26. 17:13

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ”๋ฌธ์ œ

https://leetcode.com/problems/last-person-to-fit-in-the-bus/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Table : Queue

There is a queue of people waiting to board a bus. However, the bus has a weight limit of 1000 kilograms, so there may be some people who cannot board.

Write a solution to find the person_name of the last person that can fit on the bus without exceeding the weight limit. The test cases are generated such that the first person does not exceed the weight limit.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

๋ฒ„์Šค๋ฅผ ํƒ€๋ ค๋Š” ์‚ฌ๋žŒ๋“ค์ด ์ค„์„ ์„œ ์žˆ๋Š”๋ฐ ๋ฒ„์Šค๊ฐ€ ํƒ‘์Šน ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ์–ด 1000kg๋ฅผ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ ๋”์ด์ƒ ๊ณ ๊ฐ์ด ํƒ‘์Šนํ•  ์ˆ˜ ์—†๋‹ค. ์ด ๋ฒ„์Šค์— ํƒ‘์Šนํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์ง€๋ง‰ person_name์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

ํƒ‘์Šน ์ˆœ์„œ(turn)๋งˆ๋‹ค ํƒ‘์Šนํ•œ ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ์ด ๋ฌด๊ฒŒ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ

 โ‘  ์œˆ๋„์šฐ ํ•จ์ˆ˜ sum()์„ ์‚ฌ์šฉํ•ด์„œ ํƒ‘์Šน ์ˆœ์„œ์— ๋”ฐ๋ฅธ ๋ˆ„์  ๋ฌด๊ฒŒํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋Š” ํ…Œ์ด๋ธ”์„ ๋งŒ๋“  ํ›„ 

 โ‘ก ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ํƒ‘์Šน ์ˆœ์„œ์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์—ญ์ •๋ ฌํ•ด์ฃผ๊ณ 

 โ‘ข < โ‘ก> ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด  ๋ฌด๊ฒŒํ•ฉ์ด 1000์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” where์กฐ๊ฑด์„ ๋ถ€์—ฌํ•˜์—ฌ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์ถœ๋ ฅ๋˜๋Š” person_name์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

SELECT person_name
 FROM (  
    SELECT *, SUM(weight) OVER (ORDER BY turn) AS limit_wg
    FROM Queue
    ORDER BY turn DESC
 ) Q
 WHERE limit_wg <= 1000
 LIMIT 1

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leet code] 1934. Confirmation Rate  (1) 2023.10.05
[Leet code] 1393. Capital Gain/Loss  (1) 2023.10.04
1907. Count Salary Categories  (0) 2023.09.24
[Leet code] 1341. Movie Rating  (1) 2023.09.22
[Leet code] 1193. Monthly Transactions I  (0) 2023.09.21