Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8568408

Browse files
jaewon1676prove-ability
authored andcommitted
[풀이 추가] 2022年04月04日, 1문제, 이중우선순위큐.js
1 parent 8164441 commit 8568408

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎level-3/이중우선순위큐.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - jaewon1676
4+
function solution(operations) {
5+
var answer = [];
6+
for (let i = 0; i < operations.length; i++) {
7+
8+
// 숫자 삽입
9+
if (operations[i][0] == 'I') {
10+
let m = operations[i].substring(2, operations[i].length)
11+
answer.push(m)
12+
}
13+
// if 최댓값 삭제
14+
else if (operations[i][0] == 'D' && operations[i][2] == '1' && operations.length > 0) {
15+
answer.pop()
16+
}
17+
// if 최솟값 삭제
18+
else if (operations[i][0] == 'D' && operations[i][2] == '-' && operations[i][3] == '1' && operations.length > 0){
19+
20+
answer.shift()
21+
}
22+
23+
answer.sort((a, b) => {return a - b});
24+
}
25+
if (answer.length == 0) return [0, 0]
26+
else {
27+
return [parseInt(answer.pop()), parseInt(answer.shift())];
28+
}
29+
}
30+
/* 풀이 과정
31+
1. 연산 처리를 구별하기 위해 배열의 0번째 자리, 2번째 자리에 있는 등을 비교하여 조건에 따른 명령을 실행한다.
32+
2. answer 배열을 정렬 해준다
33+
3. 큐가 비어있으면 ( length == 0 ) 0을 반환. , 그렇지 않으면 [최댓값, 최솟값]을 반환한다.
34+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /