The Supermarket Queue
์ฌ๋ฐ๋ ์ค์ํ ๋๋๋๋ ๋ฌธ์
๋ฌธ์
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
queueTime([5,3,4], 1) // should return 12 // because when there is 1 till, the total time is just the sum of the times queueTime([10,2,3,3], 2) // should return 10 // because here n=2 and the 2nd, 3rd, and 4th people in the // queue finish before the 1st person has finished. queueTime([2,3,10], 2) // should return 12
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 ์ ์์๋ฅผ ํ๊ฐ์ฉ ์ถ๊ฐ ํ ๊ฒ์ด๋ค.
์ฝ๋ ๊ตฌํ
function queueTime(customers, n) {
let maxNumber = 0;
let lowNumber = 0;
let lowNumberIndex = 0;
let selfCheckout = [];
// ์๋์ด 0์ผ ๋
if (!customers.length) return 0;
// ๊ณ์ฐ๋๊ฐ 1๊ฐ ์ผ ๋
if (n === 1) return customers.reduce( (prev, curr) => prev + curr );
// ์๋๋ณด๋ค ๊ณ์ฐ๋๊ฐ ๋ ๋ง์ ๋
if (customers.length === n || customers < n) {
findMaxNumber(customers);
return maxNumber;
}
// ๊ณ์ฐ๋๋ณด๋ค ์๋์ด ๋ ๋ง์ ๋
customers.forEach((peple, index) => {
// ๋น ๊ณ์ฐ๋๊ฐ ์๋๋ก ํ๋ค.
if (index < n) {
selfCheckout.push(peple);
console.log(selfCheckout);
} else {
//์ต์ ์๊ฐ์ ๊ฐ์ง ์๋ฆฌ ์ฐพ๊ธฐ
selfCheckout.forEach((selfCheckOut, num) => {
if (!lowNumber || lowNumber > selfCheckOut) {
lowNumber = selfCheckOut;
lowNumberIndex = num;
}
});
selfCheckout[lowNumberIndex] = selfCheckout[lowNumberIndex] + peple;
lowNumber = 0;
lowNumberIndex = 0;
}
});
findMaxNumber(selfCheckout);
function findMaxNumber (arr) {
for (const val of arr) {
if (val > maxNumber) maxNumber = val;
}
}
return maxNumber;
}
๊ฒฐ๊ณผ ๋ถ์
๋๋ค ํ ์คํธ ํต๊ณผ
@Hacker Sakana, @chris_steenekamp, @VadymBoguslavsky, @cristhian.mesta's Solution
function queueTime(customers, n) {
var w = new Array(n).fill(0);
for (let t of customers) {
let idx = w.indexOf(Math.min(...w));
w[idx] += t;
}
return Math.max(...w);
}
์๋ฃจ์ ์ด ๋๋ฌด ์งง์์ ๋นํฉ์ค๋ฌ์ธ ์ง๊ฒฝ์ด๋ค. ํ์ค ํ์ค ํด์ํด ๋ณด์.
var w = new Array(n).fill(0);
new Array(n)์ ํ๊ฒ ๋๋ฉด [empty x n]์ด ๋๋ค. fill(0)์ ํตํด empty์๋ฆฌ์ 0์ด ์ฑ์์ง๋ค.
w์ ์ญํ ์ ๊ณ์ฐ๋๊ฐ ๋๋ค.
for (let t of customers) {
let idx = w.indexOf(Math.min(...w));
w[idx] += t;
}
์ฒดํฌํด๋ณด์.
Math.min(...w);
๋ฐฐ์ด์ด์๋ w๋ฅผ spread operator๋ก ๋ฐฐ์ด์ ๋ฒ๊ฒจ๋ฒ๋ ธ๋ค. :open_mouth: ์๋๋ฉด Math.min์ ์ซ์ํ๋ฐ์ ๋ชป๋ฐ๊ธฐ ๋๋ฌธ์ spread operator๊ฐ ์์ฃผ ์ ์ ํ๋ค.
let idx = w.indexOf(Math.min(...w));
indexOf()๋ ์ฃผ์ด์ง ์กฐ๊ฑด๊ณผ ์ผ์นํ๋ ์ฒซ๋ฒ์งธ index๋ฅผ ๋ฐํํ๋ค.
for (let t of customers) {
let idx = w.indexOf(Math.min(...w));
w[idx] += t;
}
customers์ ์์ ํ๋ํ๋๋ฅผ ๊ฐ์ฅ ์์ ์๋ฅผ ๊ฐ์ง w๋ฐฐ์ด์ ๋ฃ์ ์ ์๋ค!
return Math.max(...w);
๊ทธ๋ฆฌ๊ณ ๋ฆฌํด๊ฐ์ Math.max() + spread operator์ ํฉ์ณ ๋ฐฐ์ด์์ ๊ฐ์ฅ ํฐ์๋ฅผ ์ฐพ์๋ธ๋ค! ๋๋จํด ! ์ฌ๋ฐ๋ค :astonished:
Last updated
Was this helpful?