Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- CMD
- 20055
- documentationpluginsbootstrapper
- 14466
- 이산수학
- to display the conditions report re-run your application with 'debug' enabled
- 알고리즘
- Eclipse
- docker
- 2167. 2차원 배열의 합
- 21278
- 자바
- Error
- 18222
- 2108_통계학
- 호석이두마리치킨
- 백준
- Java
- dockercompose
- 소가길을건너간이유6
- 이클립스
- 투에모스문자열
- 날짜일수
- 별자리 만들기
- jenkins
- EC2
- SpringBoot
- Error fetching remote repo 'origin'
- 프로그래머스
- 설정
Archives
- Today
- Total
계단을 오르듯이
[JAVA] 프로그래머스 - 프린터 본문
문제를 이해하면 이렇다.
대기 목록에서 하나의 문서를 순서대로 빼어, 그 문서가 최고의 우선순위이면 그대로 프린트를 한다.
그 프린트의 위치가 일치하면 바로 끝.
그렇지 않으면 프린트하는 순서를 알아내기 위해 순서를 나타내는 변수를 올려준다.
만약 최고의 우선순위가 아닐 시 바로 뒤로 줄을 서야하고 그 외에는 변화할게 없다.
for-each의 for문을 이용하여 큐에 존재하는 모든 원소와 우선순위를 비교할 수 있었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<Node> q = new LinkedList<>();
for(int i=0;i<priorities.length;i++){
q.offer(new Node(i,priorities[i]));
}
int num = 0; // 순서 - 결과값
while(!q.isEmpty()){
Node now = q.poll();
boolean flag = false;
for(Node n : q){
if(n.priority > now.priority){ // 높은 우선순위가 있다면
flag = true;
break;
}
}
if(flag){ // 뒤로
q.offer(now);
}else{
num++; // 이번 순서
if(now.idx == location){
answer = num;
break;
}
}
}
return answer;
}
static class Node{
int idx, priority;
public Node(int idx, int priority){
super();
this.idx = idx;
this.priority = priority;
}
}
}
|
cs |
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[JAVA] 프로그래머스 - 주식가격 (0) | 2022.01.31 |
---|---|
[JAVA] 프로그래머스 - 다리를 지나는 트럭 (0) | 2022.01.30 |
[JAVA] 프로그래머스_베스트 앨범 (0) | 2022.01.12 |
[JAVA] 네트워크 (0) | 2022.01.08 |
[JAVA] 타겟 넘버 (0) | 2022.01.08 |