Skip to content

Commit 311424f

Browse files
committed
[level 3] Title: 선입 선출 스케줄링, Time: 3.10 ms, Memory: 51.9 MB -BaekjoonHub
1 parent 381df57 commit 311424f

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [level 3] 선입 선출 스케줄링 - 12920
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12920)
4+
5+
### 성능 요약
6+
7+
메모리: 51.9 MB, 시간: 3.10 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 70.0<br/>효율성: 30.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2024년 04월 13일 21:15:29
20+
21+
### 문제 설명
22+
23+
<p>처리해야 할 동일한 작업이 n 개가 있고, 이를 처리하기 위한 CPU가 있습니다.</p>
24+
25+
<p>이 CPU는 다음과 같은 특징이 있습니다.</p>
26+
27+
<ul>
28+
<li>CPU에는 여러 개의 코어가 있고, 코어별로 한 작업을 처리하는 시간이 다릅니다.</li>
29+
<li>한 코어에서 작업이 끝나면 작업이 없는 코어가 바로 다음 작업을 수행합니다.</li>
30+
<li>2개 이상의 코어가 남을 경우 앞의 코어부터 작업을 처리 합니다.</li>
31+
</ul>
32+
33+
<p>처리해야 될 작업의 개수 n과, 각 코어의 처리시간이 담긴 배열 cores 가 매개변수로 주어질 때, 마지막 작업을 처리하는 코어의 번호를 return 하는 solution 함수를 완성해주세요.</p>
34+
35+
<h5>제한 사항</h5>
36+
37+
<ul>
38+
<li>코어의 수는 10,000 이하 2이상 입니다.</li>
39+
<li>코어당 작업을 처리하는 시간은 10,000이하 입니다.</li>
40+
<li>처리해야 하는 일의 개수는 50,000개를 넘기지 않습니다.</li>
41+
</ul>
42+
43+
<hr>
44+
45+
<h5>입출력 예</h5>
46+
<table class="table">
47+
<thead><tr>
48+
<th>n</th>
49+
<th>cores</th>
50+
<th>result</th>
51+
</tr>
52+
</thead>
53+
<tbody><tr>
54+
<td>6</td>
55+
<td>[1,2,3]</td>
56+
<td>2</td>
57+
</tr>
58+
</tbody>
59+
</table>
60+
<h5>입출력 예 설명</h5>
61+
62+
<p>입출력 예 #1<br>
63+
처음 3개의 작업은 각각 1,2,3번에 들어가고, 1시간 뒤 1번 코어에 4번째 작업,다시 1시간 뒤 1,2번 코어에 5,6번째 작업이 들어가므로 2를 반환해주면 됩니다.</p>
64+
65+
66+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public int solution(int n, int[] cores) {
3+
int max = n * 10000;
4+
int min = 0;
5+
6+
int time = 0;
7+
int m = 0;
8+
9+
// time : 마지막 작업이 시작되는 시점을 구함
10+
while (min <= max) {
11+
int mid = (min + max) / 2;
12+
int count = calculate(mid, cores);
13+
14+
if (count >= n) {
15+
max = mid - 1;
16+
time = mid;
17+
m = count;
18+
} else {
19+
min = mid + 1;
20+
}
21+
}
22+
23+
// n보다 추가로 처리한 작업량
24+
int remain = m - n;
25+
int answer = 0;
26+
27+
for (int i = cores.length - 1; i >= 0; i--) {
28+
if (time % cores[i] == 0) {
29+
if (remain == 0) {
30+
answer = i + 1;
31+
break;
32+
}
33+
remain--;
34+
}
35+
}
36+
37+
return answer;
38+
}
39+
40+
private int calculate(int time, int[] cores) {
41+
if (time == 0) {
42+
return cores.length;
43+
}
44+
45+
int count = cores.length;
46+
for (int c : cores) {
47+
count += (time / c);
48+
}
49+
return count;
50+
}
51+
}

0 commit comments

Comments
 (0)