https://www.codewars.com/kata/57b06f90e298a7b53d000a86
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
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.
Important
Please look at the examples and clarifications below, to ensure you understand the task correctly :)
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.
N.B. You should assume that all the test input will be valid, as specified above.
P.S. The situation in this kata can be likened to the more-computer-science-related idea of a thread pool, with relation to running multiple processes at the same time: https://en.wikipedia.org/wiki/Thread_pool
설명:
슈퍼마켓에 셀프 계산대를 위한 대기 행렬이 있습니다. 모든 사람이 계산을 마치는데 걸리는 총 시간을 계산하는 함수를 만드세요!
입력
- 고객: 대기 행렬을 대표하는 양의 정수로 이루어진 배열. 각 정수는 고객을 대신하고 그것의 값은 그들이 계산을 마치는데 필요한 시간을 의미합니다.
- n: 양의 정수, 계산대의 수입니다.
출력
함수는 필요한 총 시간인 정수를 반환해야 합니다.
중요
작업을 정확하게 이해하고 있는지 확인하려면 아래의 예시와 설명을 보세요 :)
예시
queueTime([5,3,4], 1)
// 12를 반환해야 합니다.
// 계산대가 1개라면, 총 시간은 시간의 합일 뿐입니다.
queueTime([10,2,3,3], 2)
// 10을 반환해야 합니다.
// n=2이고 대기열의 2번째, 3번째, 그리고 4번째 사람은
// 첫 번째 사람이 끝나기 전에 끝나기 때문입니다.
queueTime([2,3,10], 2)
// 12를 반환해야 합니다.
설명
- 많은 계산대를 제공하는 대기열은 하나뿐입니다.
- 대기열의 수는 절대 바뀌지 않습니다.
- 대기열의 가장 앞 사람은 (즉, 배열/목록의 첫번째 원소) 계산대가 비게되면 바로 이동합니다.
주의: 위에 명시된대로 모든 테스트 입력이 유효하다고 가정해야 합니다.
추신: 이 kata의 상황은 동시에 여러 프로세스를 작동하는 것과 관련한 스레드 풀에 대한 컴퓨터 과학 관련 아이디어에 비유할 수 있습니다: https://en.wikipedia.org/wiki/Thread_pool
풀이
계산대의 수 크기의 배열을 만들고 0으로 초기화를 한다. 그리고 반복문으로 가장 작은 값을 가지고 있는 원소의 위치에 배열의 값을 더해주었다. 그리고 완성된 배열의 가장 큰 값을 출력해주었다.
코드
function queueTime(customers, n) {
let result = Array.from({length: n}, () => 0);
for(let i = 0; i < customers.length; i++){
let index = result.indexOf(Math.min(...result));
result[index] += customers[i];
}
return Math.max(...result);
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] If you can't sleep, just count sheep!! (8 kyu) / JavaScript (0) | 2022.11.04 |
---|---|
[Codewars] Calculate average (8 kyu) / JavaScript (0) | 2022.11.03 |
[Codewars] Count of positives / sum of negatives (8 kyu) / JavaScript (0) | 2022.11.01 |
[Codewars] Friend or Foe? (7 kyu) / JavaScript (0) | 2022.10.31 |
[Codewars] Replace With Alphabet Position (6 kyu) / JavaScript (0) | 2022.10.30 |
댓글