The Supermarket Queue

CodeWars Linkarrow-up-right

μž¬λ°ŒλŠ” μ‹€μƒν™œ λŠλ‚Œλ‚˜λŠ” 문제

문제

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 의 μš”μ†Œλ₯Ό ν•œκ°œμ”© μΆ”κ°€ ν•  것이닀.

μ½”λ“œ κ΅¬ν˜„

κ²°κ³Ό 뢄석

랜덀 ν…ŒμŠ€νŠΈ 톡과

@Hacker Sakanaarrow-up-right, @chris_steenekamparrow-up-right, @VadymBoguslavskyarrow-up-right, @cristhian.mestaarrow-up-right's Solution

μ†”λ£¨μ…˜μ΄ λ„ˆλ¬΄ μ§§μ•„μ„œ λ‹Ήν™©μŠ€λŸ¬μšΈ 지경이닀. ν•œμ€„ ν•œμ€„ 해석해 보자.

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:

Last updated