The Supermarket Queue

CodeWars Link

์žฌ๋ฐŒ๋Š” ์‹ค์ƒํ™œ ๋А๋‚Œ๋‚˜๋Š” ๋ฌธ์ œ

๋ฌธ์ œ

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?