Skip to content

Commit 4b059ab

Browse files
committed
Merge branch 'main' of https://github.com/ho0010/algorithm
2 parents c75755c + fa567a3 commit 4b059ab

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [Silver IV] 스택 - 10828
2+
3+
[문제 링크](https://www.acmicpc.net/problem/10828)
4+
5+
### 성능 요약
6+
7+
메모리: 12088 KB, 시간: 168 ms
8+
9+
### 분류
10+
11+
자료 구조, 구현, 스택
12+
13+
### 제출 일자
14+
15+
2025년 3월 20일 16:39:10
16+
17+
### 문제 설명
18+
19+
<p>정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.</p>
20+
21+
<p>명령은 총 다섯 가지이다.</p>
22+
23+
<ul>
24+
<li>push X: 정수 X를 스택에 넣는 연산이다.</li>
25+
<li>pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.</li>
26+
<li>size: 스택에 들어있는 정수의 개수를 출력한다.</li>
27+
<li>empty: 스택이 비어있으면 1, 아니면 0을 출력한다.</li>
28+
<li>top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.</li>
29+
</ul>
30+
31+
### 입력
32+
33+
<p>첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.</p>
34+
35+
### 출력
36+
37+
<p>출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.</p>
38+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
2+
let N = Number(input[0]);
3+
4+
let answer = [];
5+
let stack = [];
6+
for (let i = 1; i <= N; i++) {
7+
if (input[i].includes('push')) {
8+
stack.push(input[i].split(' ')[1]);
9+
} else if (input[i].includes('pop')) {
10+
answer.push(stack.length === 0 ? -1 : stack.pop());
11+
} else if (input[i].includes('size')) {
12+
answer.push(stack.length);
13+
} else if (input[i].includes('empty')) {
14+
answer.push(stack.length === 0 ? 1 : 0);
15+
} else {
16+
answer.push(stack.length === 0 ? -1 : stack[stack.length - 1]);
17+
}
18+
}
19+
console.log(answer.join('\n'));
20+
21+
// push, pop 이용해서 구현
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [Silver IV] 큐 - 10845
2+
3+
[문제 링크](https://www.acmicpc.net/problem/10845)
4+
5+
### 성능 요약
6+
7+
메모리: 12016 KB, 시간: 164 ms
8+
9+
### 분류
10+
11+
자료 구조, 큐
12+
13+
### 제출 일자
14+
15+
2025년 3월 21일 09:51:02
16+
17+
### 문제 설명
18+
19+
<p>정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.</p>
20+
21+
<p>명령은 총 여섯 가지이다.</p>
22+
23+
<ul>
24+
<li>push X: 정수 X를 큐에 넣는 연산이다.</li>
25+
<li>pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.</li>
26+
<li>size: 큐에 들어있는 정수의 개수를 출력한다.</li>
27+
<li>empty: 큐가 비어있으면 1, 아니면 0을 출력한다.</li>
28+
<li>front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.</li>
29+
<li>back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.</li>
30+
</ul>
31+
32+
### 입력
33+
34+
<p>첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.</p>
35+
36+
### 출력
37+
38+
<p>출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.</p>
39+

백준/Silver/10845. 큐/큐.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
2+
let answer = [];
3+
let queue = [];
4+
for (let i = 1; i < input.length; i++) {
5+
if (input[i].includes('push')) queue.push(input[i].split(' ')[1]);
6+
else if (input[i].includes('pop')) answer.push(queue.length === 0 ? -1 : queue.shift());
7+
else if (input[i].includes('size')) answer.push(queue.length);
8+
else if (input[i].includes('empty')) answer.push(queue.length === 0 ? 1 : 0);
9+
else if (input[i].includes('front')) answer.push(queue.length === 0 ? -1 : queue[0]);
10+
else answer.push(queue.length === 0 ? -1 : queue[queue.length - 1]);
11+
}
12+
console.log(answer.join('\n'));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [Silver V] 집합 - 11723
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11723)
4+
5+
### 성능 요약
6+
7+
메모리: 2024 KB, 시간: 576 ms
8+
9+
### 분류
10+
11+
비트마스킹, 구현
12+
13+
### 제출 일자
14+
15+
2025년 3월 24일 20:46:08
16+
17+
### 문제 설명
18+
19+
<p>비어있는 공집합 S가 주어졌을 때, 아래 연산을 수행하는 프로그램을 작성하시오.</p>
20+
21+
<ul>
22+
<li><code>add x</code>: S에 x를 추가한다. (1 ≤ x ≤ 20) S에 x가 이미 있는 경우에는 연산을 무시한다.</li>
23+
<li><code>remove x</code>: S에서 x를 제거한다. (1 ≤ x ≤ 20) S에 x가 없는 경우에는 연산을 무시한다.</li>
24+
<li><code>check x</code>: S에 x가 있으면 1을, 없으면 0을 출력한다. (1 ≤ x ≤ 20)</li>
25+
<li><code>toggle x</code>: S에 x가 있으면 x를 제거하고, 없으면 x를 추가한다. (1 ≤ x ≤ 20)</li>
26+
<li><code>all</code>: S를 {1, 2, ..., 20} 으로 바꾼다.</li>
27+
<li><code>empty</code>: S를 공집합으로 바꾼다.</li>
28+
</ul>
29+
30+
### 입력
31+
32+
<p>첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다.</p>
33+
34+
<p>둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.</p>
35+
36+
### 출력
37+
38+
<p><code>check</code> 연산이 주어질때마다, 결과를 출력한다.</p>
39+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
int main() {
7+
ios::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
10+
int M;
11+
cin >> M;
12+
13+
int bit = 0;
14+
15+
while (M--) {
16+
string cmd;
17+
int x;
18+
cin >> cmd;
19+
20+
if (cmd == "add") {
21+
cin >> x;
22+
bit |= (1 << x);
23+
} else if (cmd == "remove") {
24+
cin >> x;
25+
bit &= ~(1 << x);
26+
} else if (cmd == "check") {
27+
cin >> x;
28+
cout << ((bit & (1 << x)) ? 1 : 0) << '\n';
29+
} else if (cmd == "toggle") {
30+
cin >> x;
31+
bit ^= (1 << x);
32+
} else if (cmd == "all") {
33+
bit = (1 << 21) - 1;
34+
} else if (cmd == "empty") {
35+
bit = 0;
36+
}
37+
}
38+
39+
return 0;
40+
}
41+
42+
// js는 제출 불가 (메모리 초과이슈)
43+
// 비트마스크 사용
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Silver IV] 카드2 - 2164
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2164)
4+
5+
### 성능 요약
6+
7+
메모리: 89020 KB, 시간: 360 ms
8+
9+
### 분류
10+
11+
자료 구조, 큐
12+
13+
### 제출 일자
14+
15+
2025년 3월 18일 22:28:20
16+
17+
### 문제 설명
18+
19+
<p>N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, 1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다.</p>
20+
21+
<p>이제 다음과 같은 동작을 카드가 한 장 남을 때까지 반복하게 된다. 우선, 제일 위에 있는 카드를 바닥에 버린다. 그 다음, 제일 위에 있는 카드를 제일 아래에 있는 카드 밑으로 옮긴다.</p>
22+
23+
<p>예를 들어 N=4인 경우를 생각해 보자. 카드는 제일 위에서부터 1234 의 순서로 놓여있다. 1을 버리면 234가 남는다. 여기서 2를 제일 아래로 옮기면 342가 된다. 3을 버리면 42가 되고, 4를 밑으로 옮기면 24가 된다. 마지막으로 2를 버리고 나면, 남는 카드는 4가 된다.</p>
24+
25+
<p>N이 주어졌을 때, 제일 마지막에 남게 되는 카드를 구하는 프로그램을 작성하시오.</p>
26+
27+
### 입력
28+
29+
<p>첫째 줄에 정수 N(1 ≤ N ≤ 500,000)이 주어진다.</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 남게 되는 카드의 번호를 출력한다.</p>
34+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const input = require('fs').readFileSync('/dev/stdin').toString().trim();
2+
const num = parseInt(input);
3+
4+
class Node {
5+
constructor(value) {
6+
this.value = value;
7+
this.next = null;
8+
this.prev = null;
9+
}
10+
}
11+
12+
class LinkedList {
13+
constructor() {
14+
this.head = null;
15+
this.tail = null;
16+
this.length = 0;
17+
}
18+
19+
add(value) {
20+
const node = new Node(value);
21+
22+
if (this.head) {
23+
this.tail.next = node;
24+
node.prev = this.tail;
25+
} else {
26+
this.head = node;
27+
}
28+
this.tail = node;
29+
this.length++;
30+
return node;
31+
}
32+
33+
remove() {
34+
this.head = this.head.next;
35+
this.head.prev = null;
36+
this.length--;
37+
}
38+
39+
getHead() {
40+
return this.head.value;
41+
}
42+
}
43+
44+
function solution(num) {
45+
const cards = new LinkedList();
46+
for (let i = 1; i <= num; i++) {
47+
cards.add(i);
48+
}
49+
50+
while (cards.length > 1) {
51+
cards.remove();
52+
cards.add(cards.getHead());
53+
cards.remove();
54+
}
55+
return cards.getHead();
56+
}
57+
58+
const card = solution(num);
59+
console.log(card);
60+
61+
// 양방향 연결리스트가 아니면 시간복잡도 측면에서 해결하기 힘든 문제

0 commit comments

Comments
 (0)