The Supermarket Queue
Last updated
Was this helpful?
Last updated
Was this helpful?
์ฌ๋ฐ๋ ์ค์ํ ๋๋๋๋ ๋ฌธ์
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
Input
customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
Output
The function should return an integer, the total time required.
Examples
Clarifications
There is only ONE queue serving many tills, and
The order of the queue NEVER changes, and
The front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
customers []๋ ์ธ์ ํ๋ํ๋๊ฐ ์๋์ด ๊ณ์ฐํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด๋ค. n์ ์ ํ ๊ณ์ฐ๋์ ์ ๋ง์ฝ customers๋ [10,2,3,3] ์ด๊ณ n์ด 2๋ผ๋ฉด,
๊ณ์ฐ๋1
๊ณ์ฐ๋2
10
2
3
3
์ด 10์๊ฐ์ด ๋๋ค.
๋งค๊ฐ๋ณ์ customers: ๊ณ ๊ฐ๋ณ ์ ํ ์ฒดํฌ์์ ์ฒ๋ฆฌ ์๊ฐ
n: ์ ํ๊ณ์ฐ๋์ ์
customers === []๋ 0์ returnํ๋ค.
n === 1 ์ผ ๋๋ customers์ ์์๋ฅผ ๋ ํ๋ค.
customers.length === n || customers.length < n ์ผ ๋ customers ์์์ค ๊ฐ์ฅ ์์ ์๋ฅผ ๊ตฌํ๋ค.
customers.length > n ์ผ ๋๋ ์๋ก์ด ๋ฐฐ์ด์ ํตํด ์๋ก์ด ๋ฐฐ์ด์ length = n๊ณผ ๊ฐ๊ณ ์์์๋ ๊ฐ์ฅ ์์ ์์ customers ์ ์์๋ฅผ ํ๊ฐ์ฉ ์ถ๊ฐ ํ ๊ฒ์ด๋ค.
๋๋ค ํ ์คํธ ํต๊ณผ
์๋ฃจ์ ์ด ๋๋ฌด ์งง์์ ๋นํฉ์ค๋ฌ์ธ ์ง๊ฒฝ์ด๋ค. ํ์ค ํ์ค ํด์ํด ๋ณด์.
new Array(n)์ ํ๊ฒ ๋๋ฉด [empty x n]์ด ๋๋ค. fill(0)์ ํตํด empty์๋ฆฌ์ 0์ด ์ฑ์์ง๋ค.
w์ ์ญํ ์ ๊ณ์ฐ๋๊ฐ ๋๋ค.
์ฒดํฌํด๋ณด์.
๋ฐฐ์ด์ด์๋ w๋ฅผ spread operator๋ก ๋ฐฐ์ด์ ๋ฒ๊ฒจ๋ฒ๋ ธ๋ค. :open_mouth: ์๋๋ฉด Math.min์ ์ซ์ํ๋ฐ์ ๋ชป๋ฐ๊ธฐ ๋๋ฌธ์ spread operator๊ฐ ์์ฃผ ์ ์ ํ๋ค.
indexOf()๋ ์ฃผ์ด์ง ์กฐ๊ฑด๊ณผ ์ผ์นํ๋ ์ฒซ๋ฒ์งธ index๋ฅผ ๋ฐํํ๋ค.
customers์ ์์ ํ๋ํ๋๋ฅผ ๊ฐ์ฅ ์์ ์๋ฅผ ๊ฐ์ง w๋ฐฐ์ด์ ๋ฃ์ ์ ์๋ค!
๊ทธ๋ฆฌ๊ณ ๋ฆฌํด๊ฐ์ Math.max() + spread operator์ ํฉ์ณ ๋ฐฐ์ด์์ ๊ฐ์ฅ ํฐ์๋ฅผ ์ฐพ์๋ธ๋ค! ๋๋จํด ! ์ฌ๋ฐ๋ค :astonished:
, , , 's Solution