HIT해
[JS 백준] 28279.덱 2 본문
728x90
https://www.acmicpc.net/problem/28279
28279번: 덱 2
첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000) 둘째 줄부터 N개 줄에 명령이 하나씩 주어진다. 출력을 요구하는 명령은 하나 이상 주어진다.
www.acmicpc.net
아래는 내 첫 풀이이다.
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';
const input = require("fs")
.readFileSync(filePath)
.toString()
.trim()
.split("\n");
let TC = input.shift();
//console.log(input);
let deck = [];
for(let i = 0; i < TC; i++){
let tmp = input[i].split(" ").map(Number);
let act = tmp.shift();
if(act === 1){
deck.unshift(tmp.shift());
}else if(act === 2){
deck.push(tmp.shift());
}
else if(act === 3){
if(deck.length === 0){
console.log("-1");
}else{
console.log(deck.shift());
}
}
else if(act === 4){
if(deck.length === 0){
console.log("-1");
}else{
console.log(deck.pop());
}
}
else if(act === 5){
console.log(deck.length);
}
else if(act === 6){
if(deck.length === 0){
console.log(1);
}else{
console.log(0);
}
}
else if(act === 7){
if(deck.length === 0){
console.log(-1);
}else{
console.log(deck[0]);
}
}
else if(act === 8){
if(deck.length === 0){
console.log(-1);
}else{
console.log(deck[deck.length-1]);
}
}
}
시간초과로 틀리고 말았다.
배운점
1. 1 6 과 같이 띄워져있는 입력값에서 .map(Number) 로 처리하게되면 NaN으로 처리된다.
2. shift와 unshift 연산은 시간이 오래 걸린다.
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';
const [N, ...input] = require("fs")
.readFileSync(filePath)
.toString()
.trim()
.split("\n");
const n = 1000000; // 문제에서 주어진 덱 크기
let output = "";
const deck = new Array(n); // 100000 크기만큼 배열 생성
let size = 0;
let front = -1;
let rear = -1;
const addFirst = (x) =>{
if (size === 0){ // 아직 아무것도 들어오지 않았을 때
front = 0;
rear = 0;
}else{
if( front === 0){
front = n - 1; // 왜 100000-1 을 하는걸까.. 이해완
}else{
front--;
}
}
deck[front] = x;
size++;
}
const addLast = (x) => {
if(size === 0){
front = 0;
rear = 0;
}else{
if(rear === n-1){
rear = 0;
}else{
rear++;
}
}
deck[rear] = x;
size++;
}
const removeFirst = ()=>{
output += deck[front] + "\n";
size--;
if(front === n-1){ // 덱의 길이 100000을 넘었을때 다시 0으로 돌려줘야함
front = 0;
}else{
front++;
}
}
const removeLast = () =>{
output += deck[rear] + "\n";
size--;
if(rear === 0){
rear = n - 1;
}else{
rear--;
}
}
input.forEach((x) => {
const [act, num] = x.split(" ").map(Number);
if (act === 1) {
addFirst(num);
}
if (act === 2) {
addLast(num);
}
if (act === 3) {
size === 0 ? (output += "-1\n") : removeFirst();
}
if (act === 4) {
size === 0 ? (output += "-1\n") : removeLast();
}
if (act === 5) {
output += size + "\n";
}
if (act === 6) {
size === 0 ? (output += "1\n") : (output += "0\n");
}
if (act === 7) {
size === 0 ? (output += "-1\n") : (output += deck[front] + "\n");
}
if (act === 8) {
size === 0 ? (output += "-1\n") : (output += deck[rear] + "\n");
}
});
console.log(output);
https://arnopark.tistory.com/838
[백준/nodeJS] 28279. 덱 2
안녕하세요. 박기린 입니다. 백준 28279번 덱 2 문제를 풀어보겠습니다. 문제 링크 https://www.acmicpc.net/problem/28279 28279번: 덱 2 첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000) 둘째 줄부터 N개
arnopark.tistory.com
이분의 풀이를 보고 클론코딩을 했다.
구조분할할당과 String 변수에 정답을 담고 한번에 console.log 로 출력하는 것이 깔끔했다.
'Vue > JavaScript 알고리즘' 카테고리의 다른 글
[JS 백준] 15649.N과 M (1) (0) | 2024.01.08 |
---|---|
[JS 백준] 11866.요세푸스 문제 0 (0) | 2024.01.07 |
[JS 백준] 9012.괄호 (0) | 2024.01.07 |
[JS 백준] 1929.소수 구하기 (0) | 2024.01.06 |
[JS 백준] 1978.소수찾기 (0) | 2024.01.05 |