๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • ๋ฌธ์ œ
  • ๋ฌธ์ œ ์ดํ•ด
  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
  • ์ฝ”๋“œ ๊ตฌํ˜„
  • ๊ฒฐ๊ณผ ๋ถ„์„

Was this helpful?

  1. algorithm

The Supermarket Queue

PreviousalgorithmNextFind the odd int

Last updated 5 years ago

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

    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;
}

๊ฒฐ๊ณผ ๋ถ„์„

๋žœ๋ค ํ…Œ์ŠคํŠธ ํ†ต๊ณผ

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:

, , , 's Solution

CodeWars Link
@Hacker Sakana
@chris_steenekamp
@VadymBoguslavsky
@cristhian.mesta