Skip to content

Commit 2adca4f

Browse files
committed
[level 2] Title: 프로세스, Time: 1.34 ms, Memory: 90.1 MB -BaekjoonHub
1 parent b098c24 commit 2adca4f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [level 2] 프로세스 - 42587
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/42587?language=java)
4+
5+
### 성능 요약
6+
7+
메모리: 90.1 MB, 시간: 1.34 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 스택/큐
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 02월 12일 23:14:33
20+
21+
### 문제 설명
22+
23+
<p>운영체제의 역할 중 하나는 컴퓨터 시스템의 자원을 효율적으로 관리하는 것입니다. 이 문제에서는 운영체제가 다음 규칙에 따라 프로세스를 관리할 경우 특정 프로세스가 몇 번째로 실행되는지 알아내면 됩니다.</p>
24+
<div class="highlight"><pre class="codehilite"><code>1. 실행 대기 큐(Queue)에서 대기중인 프로세스 하나를 꺼냅니다.
25+
2. 큐에 대기중인 프로세스 중 우선순위가 더 높은 프로세스가 있다면 방금 꺼낸 프로세스를 다시 큐에 넣습니다.
26+
3. 만약 그런 프로세스가 없다면 방금 꺼낸 프로세스를 실행합니다.
27+
3.1 한 번 실행한 프로세스는 다시 큐에 넣지 않고 그대로 종료됩니다.
28+
</code></pre></div>
29+
<p>예를 들어 프로세스 4개 [A, B, C, D]가 순서대로 실행 대기 큐에 들어있고, 우선순위가 [2, 1, 3, 2]라면 [C, D, A, B] 순으로 실행하게 됩니다. </p>
30+
31+
<p>현재 실행 대기 큐(Queue)에 있는 프로세스의 중요도가 순서대로 담긴 배열 <code>priorities</code>와, 몇 번째로 실행되는지 알고싶은 프로세스의 위치를 알려주는 <code>location</code>이 매개변수로 주어질 때, 해당 프로세스가 몇 번째로 실행되는지 return 하도록 solution 함수를 작성해주세요.</p>
32+
33+
<hr>
34+
35+
<h5>제한사항</h5>
36+
37+
<ul>
38+
<li><code>priorities</code>의 길이는 1 이상 100 이하입니다.
39+
40+
<ul>
41+
<li><code>priorities</code>의 원소는 1 이상 9 이하의 정수입니다.</li>
42+
<li><code>priorities</code>의 원소는 우선순위를 나타내며 숫자가 클 수록 우선순위가 높습니다.</li>
43+
</ul></li>
44+
<li><code>location</code>은 0 이상 (대기 큐에 있는 프로세스 수 - 1) 이하의 값을 가집니다.
45+
46+
<ul>
47+
<li><code>priorities</code>의 가장 앞에 있으면 0, 두 번째에 있으면 1 … 과 같이 표현합니다.</li>
48+
</ul></li>
49+
</ul>
50+
51+
<h5>입출력 예</h5>
52+
<table class="table">
53+
<thead><tr>
54+
<th>priorities</th>
55+
<th>location</th>
56+
<th>return</th>
57+
</tr>
58+
</thead>
59+
<tbody><tr>
60+
<td>[2, 1, 3, 2]</td>
61+
<td>2</td>
62+
<td>1</td>
63+
</tr>
64+
<tr>
65+
<td>[1, 1, 9, 1, 1, 1]</td>
66+
<td>0</td>
67+
<td>5</td>
68+
</tr>
69+
</tbody>
70+
</table>
71+
<h5>입출력 예 설명</h5>
72+
73+
<p>예제 #1</p>
74+
75+
<p>문제에 나온 예와 같습니다.</p>
76+
77+
<p>예제 #2</p>
78+
79+
<p>6개의 프로세스 [A, B, C, D, E, F]가 대기 큐에 있고 중요도가 [1, 1, 9, 1, 1, 1] 이므로 [C, D, E, F, A, B] 순으로 실행됩니다. 따라서 A는 5번째로 실행됩니다.</p>
80+
81+
<hr>
82+
83+
<p>※ 공지 - 2023년 4월 21일 문제 지문이 리뉴얼되었습니다.</p>
84+
85+
86+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[] priorities, int location) {
5+
int answer = 0;
6+
Deque<int[]> queue = new ArrayDeque<>();
7+
for(int i = 0; i < priorities.length; i++) {
8+
queue.add(new int[]{i, priorities[i]});
9+
}
10+
11+
while(!queue.isEmpty()) {
12+
int[] cur = queue.poll();
13+
int curIdx = cur[0];
14+
int curPriority = cur[1];
15+
16+
if(isGreater(curPriority, queue)) {
17+
queue.add(cur);
18+
} else {
19+
answer++;
20+
21+
if(curIdx == location) {
22+
return answer;
23+
}
24+
}
25+
26+
}
27+
return answer;
28+
}
29+
30+
private boolean isGreater(int priority, Deque<int[]> queue) {
31+
for(int[] q : queue) {
32+
if(q[1] > priority) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
}

0 commit comments

Comments
 (0)