Algorithm

Help Suzuki complete his chores!

2020.08.131분 읽기

문제 설명

Suzuki has a long list of chores required to keep the monastery in good order. Each chore can be completed independent of the others and assigned to any student. He needs to assign two chores to each student in a way which minimizes the total duration of the day's work. There will always be an even number of chores and enough students to complete them.


You will be given an array containing the estimated duration of each chore such as:

10chores length30
 
chores = [1, 5, 2, 8, 4, 9, 6, 4, 2, 2, 2, 9]		

Return an array with each students work duration in ascending order such that the last element represents the total duration of the day's work. Each integer in the return array is the sum of each students two assigned chores.

chores = [1, 5, 2, 8, 4, 9, 6, 4, 2, 2, 2, 9]
 
Chore 1 = 2hrs 
Chore 2 = 9hrs
 
2 + 9 = 11 hours duration
 
return [7, 8, 8, 10, 10, 11]


풀이

export function choreAssignment(chores) {
  chores.sort((a, b) => a - b);
  const condition: number = (chores.length / 2);
  const result: number[] = [];
 
  for (let i = 0; i < condition; i++) {
    result.push(chores.splice(0, 1)[0] + chores.pop());
  }
 
  return result.sort((a, b) => a - b);
}

관련 포스트

Giscus 댓글 영역 (GitHub Discussions 연동 예정)