-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy path프린터&42587&.js
More file actions
157 lines (138 loc) · 3.98 KB
/
프린터&42587&.js
File metadata and controls
157 lines (138 loc) · 3.98 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//https://github.com/codeisneverodd/programmers-coding-test
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(priorities, location) {
var answer = 0;
let documents = priorities.map((priority, index) => ({location: index, priority: priority}))
let locationPrinted = false
while (!locationPrinted) {
const shifted = documents.shift()
let printAvailable = true
if (documents.some((document) => shifted.priority < document.priority)) printAvailable = false
if (printAvailable) {
answer += 1
if (shifted.location === location) locationPrinted = true
} else {
documents.push(shifted)
}
}
return answer;
}
//정답 2 - codeisneverodd
function solution(priorities, location) {
var answer = 0;
let documents = priorities.map((priority, documentLocation) => [documentLocation, priority])
let locationPrinted = false
while (!locationPrinted) {
const shifted = documents.shift()
let printAvailable = true
for (let i = 0; i < documents.length; i++) {
if (shifted[1] < documents[i][1]) {
printAvailable = false
break
}
}
if (printAvailable) {
answer += 1
if (shifted[0] === location) locationPrinted = true
} else {
documents.push(shifted)
}
}
return answer;
}
//정답 3 - jaewon1676
function solution(priorities, location) {
var answer = 0;
while (true) {
if (priorities[0] < Math.max(...priorities)) {
if (location - 1 < 0) location = priorities.length
priorities.push(priorities.shift())
location--;
} else {
answer++;
if (location - 1 < 0) {
return answer;
}
priorities.shift()
location--;
}
console.log(priorities, location, answer)
}
return answer
}
// 정답 4 - createhb21
function solution(priorities, location) {
var answer = priorities.map((priority, index) => {
return {
index,
priority
};
});
let queue = [];
while(answer.length > 0){
const first = answer.shift();
const isPriority = answer.some((p) => p.priority > first.priority);
isPriority ? answer.push(first) : queue.push(first);
}
const idx = queue.findIndex(p => p.index === location) + 1;
return idx;
}
//정답 5 - codeisneverodd
//shift를 사용하지 않고 queue를 구현한 풀이를 추가합니다.
function solution(priorities, location) {
let answer = 0;
const printer = new Queue;
priorities.forEach((priority, index) => {
printer.enqueue([priority, index])
})
while (printer.size() > 0) {
const check = printer.dequeue()
const countHigherPriority = printer.queue.filter(x => x[0] > check[0]).length
if (countHigherPriority > 0) {
printer.enqueue(check)
} else {
answer += 1
if (check[1] === location) break
}
}
return answer;
}
class Queue {
constructor() {
this.queue = []
this.front = 0
this.rear = 0
}
enqueue(value) {
this.queue[this.rear++] = value
}
dequeue() {
const value = this.queue[this.front]
delete this.queue[this.front]
this.front += 1
return value
}
peek() {
return this.queue(this.front)
}
size() {
return this.rear - this.front
}
}
// 정답 6 ryan-dia
function solution(priorities, location, result = 0) {
const queue = priorities.map((p, i) => ({ priority: p, index: i }));
while (queue.length > 0) {
const current = queue.shift();
const higherPriority = queue.some((d) => d.priority > current.priority);
if (higherPriority) {
queue.push(current);
continue;
}
result += 1;
if (current.index === location) {
return result;
}
}
}