티스토리 뷰
https://www.acmicpc.net/problem/1021
1021번: 회전하는 큐
첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가
www.acmicpc.net
어떤 경우에 앞에서 뒤로 보내야되는지, 뒤에서 앞으로 보내야되는지 잘 모르는데 뽀록으로 맞춤.
deque.size()+1>=2*position[0]
이부분 예제 테스트케이스 없었으면 못 맞췄을 듯 . 다시 공부 필요함.
const fs = require('fs');
const [a,b] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const [N,K] = a.split(' ').map(v=>+v);
const target = b.split(' ').map(v=>+v)
class Node{
constructor(item){
this.item = item;
this.prev = null;
this.next = null;
}
}
class Deque{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push_front(item){
const node = new Node(item);
if(this.size()==0){
this.head = node;
this.tail = node;
}else{
this.head.prev = node;
node.next=this.head;
this.head = node;
}
this.length+=1;
}
push_back(item){
const node = new Node(item)
if(this.size()==0){
this.head = node;
this.tail = node;
}else{
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
this.length +=1;
}
pop_front(){
if(this.size()==0) return -1;
const popItem = this.head;
this.head = this.head.next;
if(this.size()==1){
this.head = null;
}else{
this.head.prev = null;
}
this.length -=1;
return popItem.item;
}
pop_back(){
if(this.size()==0) return -1;
const popItem = this.tail;
this.tail = this.tail.prev;
if(this.size()==1){
this.tail = null;
}else{
this.tail.next = null;
}
this.length -=1;
return popItem.item;
}
size(){
return this.length;
}
empty(){
if(this.length==0){
return 1;
}else{
return 0;
}
}
front(){
if(this.empty()==1) return -1;
return this.head.item;
}
back(){
if(this.empty()==1) return -1;
return this.tail.item;
}
}
let answer = 0;
let deque = new Deque();
for(let i = 1; i<=N; i++){
deque.push_back(i);
}
let position = [];
for(let i = 0; i<K; i++){
position.push(target[i])
}
while(position.length>0){
if(position[0]==1){
position = position.map(v=>v-1);
deque.pop_front();
position.shift();
}else if(position[0]==deque.size()){
answer++;
deque.pop_front();
position.shift();
}else{
if(deque.size()+1>=2*position[0]){
position = position.map(v=>{
if(v==1){
return v=deque.size();
}
return v-1;
});
deque.push_back(deque.pop_front());
}else{
position = position.map(v=>{
if(v==deque.size()){
return v=1;
}
return v+1;
});
deque.push_front(deque.pop_back());
}
answer++;
}
}
console.log(answer)
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2630번 : 색종이 만들기 (0) | 2021.09.14 |
---|---|
Node.js) 백준 5430번 : AC (0) | 2021.09.14 |
Node.js) 백준 10866번 : 덱 (0) | 2021.09.13 |
Node.js) 백준 1966번 : 프린터 큐 (0) | 2021.09.13 |
Node.js) 백준 11866번 : 요세푸스 문제 0 (0) | 2021.09.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- create databases;
- 그래프
- 롱베케이션
- 은둔청년체험
- 최소공통조상
- 면접질문
- MOD
- 다이나믹프로그래밍
- create db
- 면접비
- 서버점검
- 서버개발
- 개발자면접
- BFS
- 다이나밍프로그래밍
- 로드나인
- node.js
- 투포인터
- 투포인터 연습
- MySQL
- DB 생성
- 동적프로그래밍
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함