const sequence = (staffs: string[], time: number[]) => {
const makeStaff = staffs.map((staff, idx) => ({ no: idx, staffName: staff, time: [] })) as {
no: number;
staffName: string;
time: number[];
}[];
time.forEach((v, idx) => {
const isStaffExist = makeStaff[idx];
if (isStaffExist) {
if (!isStaffExist.time[0]) {
isStaffExist.time.push(v);
return;
}
}
const calc = makeStaff.reduce(
(acc, v, idx) => {
const totalTime = v.time.reduce((acc, t) => (acc += t), 0);
if (idx === 0) {
acc = { totalTime, no: v.no };
}
if (totalTime < acc.totalTime) {
acc = { totalTime, no: v.no };
}
return acc;
},
{ totalTime: 0, no: 0 }
);
makeStaff[calc.no].time.push(v);
});
return makeStaff;
};
const result = sequence(["JohnDoe", "JaneDoe"], [4, 20, 10, 15, 3, 3, 5, 6, 7, 8, 3, 27, 8, 11, 6, 2]);
console.log(result);
상담직원한테 특정 상담시간을 갖고있는 손님을 효율적으로 분배해줌
예를 들어 두명의 직원이 있으면
처음 손님 두명은 각각 한번씩 직원no.0한테 4분짜리 손님 직원 no.1한테 20분짜리 손님이 배정되고 다음 10분짜리는 가장 상담이 빨리 끝난 no.0직원한테로 배정 직원 no.1은 20분짜리 손님이 배정되어있으니 no.0가 상담이 가장빨리 끝날것이므로 no.0한테 15분짜리 손님 배정 이런식
물론 직원이 수백명이되거나 손님이 수백명이되어도 하드코딩없이 같은 방식으로 작동할거임
칭찬해요 - dc App
무슨 알고리듬 기반이죠 // "도둑이 집을 떠나며 주인을 욕한다" - 러시아 속담