[Programmers] 프린터

Time Lapse :5min 0sec

solution.cpp

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <vector>
using namespace std;
int solution(vector<int> priorities, int location) {
int p[100], INFO[10] = {0,};
int index = 0, _P = 0, answer = 1, L = location;
int P = priorities[location], SIZE = priorities.size();
for(int i=0;i<SIZE;i++) {
if (priorities[i] >= P) {
++INFO[priorities[i]];
p[index++] = priorities[i];
_P = priorities[i] > _P ? priorities[i] : _P;
} else if (i < location)
--L;
}
SIZE = index;
index = 0;
while(_P!=P){
index = index - 1 == -1 ? SIZE-1 : index -1;
if(p[index]==_P){
answer += INFO[_P];
do{
--_P;
}while(INFO[_P]==0);
}
}
while(index!=L){
if(p[index]==P)
++answer;
index = (index+1)%SIZE;
}
return answer;
}
/* //재귀
#include <vector>
using namespace std;
int answer = 1;
int INFO[10];
int SIZE, P, L;
void DFS(vector<int> &priorities, int _P,int start){
if(_P==P){
while(start!=L){
if(priorities[start]==P)
++answer;
start = (start+1)%SIZE;
}
return ;
}
if(INFO[_P]==0){
DFS(priorities,_P-1,start);
return ;
}
answer += INFO[_P];
for(int i=0;i<SIZE;i++){
start = start - 1 == -1 ? SIZE-1 : start -1;
if(priorities[start]==_P) {
DFS(priorities,_P-1,start);
break;
}
}
}
int solution(vector<int> priorities, int location) {
SIZE = priorities.size();
P = priorities[location];
L = location;
vector<int> p(100);
int index = 0;
for(int i=0;i<SIZE;i++)
if(priorities[i]>=P) {
++INFO[priorities[i]];
p[index++] = priorities[i];
}
else if(i<location)
--L;
SIZE = p.size();
DFS(p,9,0);
return answer;
}
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/printer/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.