https://www.acmicpc.net/problem/3015
๐ ์๋ฐ(Java)๋ก ํธ๋ ์ค์์์ค ์ฌ๊ฒฐํฉ ๋ฌธ์ ํ์ด ๐
๐ ๋ฌธ์ ๊ฐ์
์ด ๋ฌธ์ ๋ ๋ฐฑ์ค 3015๋ฒ - ์ค์์์ค ์ฌ๊ฒฐํฉ ๋ฌธ์ ์
๋๋ค.
์ฃผ์ด์ง ์ฌ๋๋ค์ ํค ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ก ๋ณผ ์ ์๋ ์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์
๋๋ค.
๐ก ์์ ์ ๋ ฅ
7
2
4
1
2
2
5
1
๐ก ์์ ์ถ๋ ฅ
10
๐ ์๊ณ ๋ฆฌ์ฆ ์ ๊ทผ ๋ฐฉ์
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์คํ(Stack) ์ ํ์ฉํ O(N) ์ต์ ํ ๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
โ๏ธ ์ฃผ์ ๊ณ ๋ ค ์ฌํญ
โ ๊ฐ ์ฌ๋์ ํค(h)๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ณผ ์ ์๋ ์ฌ๋์ ์์ ๊ณ์ฐํด์ผ ํฉ๋๋ค.
โ ์คํ์ ํ์ฉํ์ฌ ํจ์จ์ ์ผ๋ก ๋ณผ ์ ์๋ ์์ ๊ณ์ฐํ ์ ์์ต๋๋ค.
โ ์คํ์๋ ํญ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ์ฅํ์ฌ, ํ์ฌ ํค๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์ฌ๋์ ๋ณผ ์ ์๋๋ก ํฉ๋๋ค.
โ ๊ฐ์ ํค๋ฅผ ๊ฐ์ง ์ฌ๋๋ค์ ํ ๋ฒ์ ์ฒ๋ฆฌํ์ฌ ์ค๋ณต ๊ณ์ฐ์ ์ค์
๋๋ค.
๐น Java ์ฝ๋ ํด์ค
import java.io.*;
import java.util.Stack;
public class Main {
static class Node {
int height;
int count;
public Node(int h, int cnt) {
this.height = h;
this.count = cnt;
}
}
static int N;
static Stack<Node> st;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
st = new Stack<>();
long answer = 0;
for (int i = 0; i < N; i++) {
int height = Integer.parseInt(br.readLine());
int count = 1;
// ์คํ์ ์ต์๋จ ๊ฐ๋ณด๋ค ์์ ๊ฒฝ์ฐ ๋ณผ ์ ์๋ ์์ ๊ณ์ฐ
while (!st.isEmpty() && st.peek().height <= height) {
Node top = st.pop();
answer += top.count;
if (top.height == height) {
count += top.count;
}
}
// ์คํ์ด ๋น์ด ์์ง ์๋ค๋ฉด ์ถ๊ฐ๋ก 1์์ ํ์ฑํ ์ ์์
if (!st.isEmpty()) {
answer++;
}
st.push(new Node(height, count));
}
System.out.println(answer);
}
}
๐ ์ฝ๋ ์ค๋ช
๐ 1. ์คํ ํ์ฉํ ๋ณผ ์ ์๋ ์ ๊ณ์ฐ
static Stack<Node> st;
- ์คํ์๋ {ํค, ๊ฐ์ ํค์ ๊ฐ์} ์ ๋ณด๋ฅผ ์ ์ฅํฉ๋๋ค.
- ์คํ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ์ง๋๋ฉฐ, ๋ฎ์์ง๋ ๊ฒฝ์ฐ ๋ณผ ์ ์๋ ์ฌ๋๋ค์ ๊ณ์ฐํ๋ฉด์ pop()ํฉ๋๋ค.
๐ 2. ํ์ฌ ํค๋ณด๋ค ์์ ํค๋ค์ ์คํ์์ ์ ๊ฑฐํ๋ฉฐ ๋ณผ ์ ์๋ ์ ๊ณ์ฐ
while (!st.isEmpty() && st.peek().height <= height) {
Node top = st.pop();
answer += top.count;
if (top.height == height) {
count += top.count;
}
}
๐น ๋์ ๋ฐฉ์
- ํ์ฌ ํค๋ณด๋ค ์์ ๊ฐ๋ค์ ์คํ์์ ์ ๊ฑฐํ๋ฉด์ ์นด์ดํธ
- ์ ๊ฑฐ๋ ํค๋ค์ ํ์ฌ ์ฌ๋์ ๋ณผ ์ ์์ผ๋ฏ๋ก answer ์ ์ถ๊ฐ
- ๊ฐ์ ํค๋ฅผ ๊ฐ์ง ๊ฒฝ์ฐ count๋ฅผ ์ฆ๊ฐ์์ผ ํ ๋ฒ์ ์ฒ๋ฆฌ
๐ 3. ์คํ์ด ๋น์ด ์์ง ์๋ค๋ฉด ์ถ๊ฐ ์ ์ถ๊ฐ
if (!st.isEmpty()) {
answer++;
}
- ์คํ์ด ๋น์ด ์์ง ์๋ค๋ฉด, ํ์ฌ ์ฌ๋์ ์ด์ ์ฌ๋์ ๋ณผ ์ ์์
- ๋ฐ๋ผ์ answer๋ฅผ +1 ์ฆ๊ฐ
๐ 4. ์๋ก์ด ๋ ธ๋ ์ถ๊ฐ
st.push(new Node(height, count));
- ํ์ฌ ์ฌ๋์ ์คํ์ ์ถ๊ฐ (ํค์ ํจ๊ป, ๊ฐ์ ํค ๊ฐ์ ์ ์ฅ)
๐ฅ ์๊ฐ ๋ณต์ก๋ ๋ถ์
์ฐ์ฐ ์ต์ ์ ๊ฒฝ์ฐ ์ํ ํ์ ์๊ฐ ๋ณต์ก๋for ๋ฃจํ | N | O(N) |
while ๋ฃจํ (์คํ์์ pop) | N | O(N) |
์ดํฉ | O(N) |
โ ๋ชจ๋ ์ฐ์ฐ์ด O(N) ์ด๋ด๋ก ์ํ๋๋ฏ๋ก ๋งค์ฐ ํจ์จ์ ์ ๋๋ค.
๐ ์ ๋ฆฌ
โ ํต์ฌ ํฌ์ธํธ
โ ์คํ์ ํ์ฉํ์ฌ ๋ณผ ์ ์๋ ์์ ํจ์จ์ ์ผ๋ก ๊ณ์ฐ
โ ๊ฐ์ ํค๋ ํ ๋ฒ์ ์ฒ๋ฆฌํ์ฌ ์ค๋ณต ๊ณ์ฐ ๋ฐฉ์ง
โ ์๊ฐ ๋ณต์ก๋๋ฅผ O(N)์ผ๋ก ์ต์ ํํ์ฌ ๋น ๋ฅด๊ฒ ํด๊ฒฐ
๐ฏ ์ด ์๊ณ ๋ฆฌ์ฆ์ ์ฅ์
โ
์คํ์ ํ์ฉํ O(N) ํ์ด๋ก ํจ์จ์
โ
๋ณผ ์ ์๋ ์์ ๋น ๋ฅด๊ฒ ํ์ ๊ฐ๋ฅ
โ
๋ค๋ฅธ "๋ณผ ์ ์๋ ์ฌ๋ ์" ๋ฌธ์ ์์๋ ํ์ฉ ๊ฐ๋ฅ
'Algorithm & Data Structures > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
b24444. ์๊ณ ๋ฆฌ์ฆ ์์ - ๋๋น ์ฐ์ ํ์ 1 (0) | 2025.03.04 |
---|---|
b24480. ์๊ณ ๋ฆฌ์ฆ ์์ - ๊น์ด ์ฐ์ ํ์ 2 (0) | 2025.03.01 |
b.1725 ํ์คํ ๊ทธ๋จ (0) | 2025.02.06 |
b.17299 ์ค๋ฑํฐ์ (1) | 2025.02.04 |
b24497. ์๊ณ ๋ฆฌ์ฆ ์์ - ๊น์ด ์ฐ์ ํ์ 1 (0) | 2025.01.27 |