BOJ 14865 - ๊ณก์ ์๋ฅด๊ธฐ [JAVA]
14865๋ฒ: ๊ณก์ ์๋ฅด๊ธฐ
์ปดํจํฐ ๊ทธ๋ํฝ ์บ๋ฒ์ค๋ ์ปดํจํฐ ํ๋ฉด์์ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋ ์ง์ฌ๊ฐํ ์์ญ์ ๋งํ๋ค. ์บ๋ฒ์ค๋ 2์ฐจ์ ์ขํ ํ๋ฉด์ฒ๋ผ ๊ฐ ์ ์ ์์น๋ฅผ ์ขํ๋ก ํ์ํ๋ค. ์บ๋ฒ์ค์ ์ ์ค์ ์ ์ด ์์ (0,0)์ด๊ณ ,
www.acmicpc.net
ํด๋น ๋ฌธ์ ๋ ์คํ(๊ดํธ ๋ฌธ์ )์ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
'์คํ'์ด๋ผ๋ ์์ด๋์ด๋ฅผ ๋ ์ฌ๋ฆฌ๊ธฐ ์ ๊น์ง ์ ๋ง ๋ง์ ์๋๋ฅผ ํ์ ๊ฒ์ด๋ค. ํ์๋ ์ฌ๋ฌ ๋ฐฉ๋ฒ(y์ถ์ ๊ธฐ์ค์ผ๋ก ์์น ๋๋ ํ๊ฐํ๋ ๊ฒฝ์ฐ, ๋ฎ๊ฐ๋ฅผ ๊ณ ๋ คํ๋ ๊ฒฝ์ฐ)์ ์๋ํด ๋ณด์๋ค. ํ์ง๋ง, ์์ธ ์ผ์ด์ค๋ค์ด ๋๋ฌด ๋ง์์ ์คํ์ด ์๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ๊ฒ์๋ ๋ง์ ์ด๋ ค์์ด ์์ ๊ฑฐ๋ผ๊ณ ์๊ฐํ๋ค.
์ฐ์ ๊ฐ์ ๋นจ๋ฆฌ ์ก๊ธฐ ์ํ์ฌ '๊ณก์ ์๋ฅด๊ธฐ' ๋ฌธ์ ์ ๊ดํธ ๋ฌธ์ ๊ฐ ์ด๋ป๊ฒ ๊ฐ์์ง ๊ทธ๋ฆผ์ ํตํด์ ์์๋ณด์.
ํ๋์ ๋ด์ฐ๋ฆฌ์ ๋ํด์ ์ผ์ชฝ ๋ถ๋ถ์ ์ด๋ฆฐ ๊ดํธ๋ก ์ค๋ฅธ์ชฝ ๋ถ๋ถ์ ๋ซํ ๊ดํธ์ด๋ค. (๊ดํธ๋ผ๋ ์์ด๋์ด๋ฅผ ์๊ฐํ์ง ๋ชปํ๋ค๊ณ ๋๋ฌด ์์ฑ ํ์ง ์๊ธฐ๋ฅผ ๋ฐ๋๋ค๐ ) ๋ํ, ์์ํ ๊ฒ์ฒ๋ผ ๋ด์ฐ๋ฆฌ๊ฐ ๊ฒน์ณ์ ธ ์๋ ๊ฒฝ์ฐ์๋ ์๋์ ๊ฐ์ด ํํ ๊ฐ๋ฅํ๋ค.
๊ดํธ๋ฅผ ๋ค ๋ง๋ค์๋ค๋ฉด ๋ฌธ์ ์์ ์๊ตฌํ๋ 2๊ฐ์ง ๊ฒฝ์ฐ์ ๊ฐ์('๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ํฌํจ๋์ง ์๋ ๋ด์ฐ๋ฆฌ'์ '๋ค๋ฅธ ๋ด์ฐ๋ฆฌ๋ฅผ ํฌํจํ์ง ์๋ ๋ด์ฐ๋ฆฌ'์ ๊ฐ์)๋ฅผ ์ฝ๊ฒ ๊ตฌํ ์ ์์ ๊ฒ์ด๋ค. ํ์ง๋ง ๋ด์ฐ๋ฆฌ์ ์ผ์ชฝ ๋ถ๋ถ(์์์ )๊ณผ ์ค๋ฅธ์ชฝ ๋ถ๋ถ(๋์ )์ ์ด๋ป๊ฒ ๊ดํธ๋ก ๋ํ๋ด๋์ง๊ฐ ์ด ๋ฌธ์ ์์ ๊ฐ์ฅ ์ด๋ ค์ด ๋ถ๋ถ์ด์ ํต์ฌ์ด๋ผ๊ณ ์๊ฐํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก ๋ฌธ์ ๋ฅผ ํ ๋ ๊ณ ๋ คํด์ผ ํ๋ 4๊ฐ์ง ์์์ ํจ๊ป ๋ฌธ์ ๋ฅผ ๊ฐ์ด ํ์ด๋ณด์.
1. ์์์ ์ฐพ๊ธฐ ๐
๋ฌธ์ ์์์ ์ฃผ์ด์ง 2๋ฒ ์์ ์ผ์ด์ค์ ๊ผญ์ง์ ('1 1', '1 -1', '-1 -1', '-1 1')์ ํตํด์ ํญ์ ์ผ์ชฝ ๋ฐ์ ๊ผญ์ง์ ์์๋ถํฐ ์์ํ๋ ๊ฒ์ด ์๋๋ผ๋ ๊ฒ์ ์ ์ ์๋ค.
๋ค์ํ ๋ฐฉ๋ฒ์ ์ด์ฉํ์ฌ ์์ ๊ผญ์ง์ ์ ์ ํ ์ ์๊ฒ ์ง๋ง ๊ฐ์ฅ ์ผ์ชฝ ๋ฐ์ ๊ผญ์ง์ ์ ์์์ ์ผ๋ก ํ์ฌ ๋ฌธ์ ๋ฅผ ํธ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข๋ค. (y์ขํ๊ฐ '์์์์ ์์ํ๋ ๊ฒฝ์ฐ'์ '์์์์ ์์ํ๋ ๊ฒฝ์ฐ'๋ฅผ ๋๋์ด ์๊ฐํด ๋ดค์ง๋ง ์์ธ ์ํฉ์ด ๋๋ฌด ๋ง๋ค.) ๊ผญ์ง์ ์ ๊ฐ์ N์ 4 ์ด์ 1,000,000 ์ดํ์ด๋ฏ๋ก ๋ฐฐ์ด์ ์ ์ฅํ ์ ์๋ค. ๊ทธ๋์ ์๋์ ๊ฐ์ด ๋ฐฐ์ด์ ๊ผญ์ง์ ๋ค์ ์ ์ฅํ๋ฉด์ ๊ผญ์ง์ ์ ์์น๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์์ธ๋ฑ์ค(startIdx)๋ฅผ ์ง์ ํ ์ ์๋ค.
xCoordinates = new int[N]; // ๊ผญ์ง์ ์ x์ขํ๋ค
yCoordinates = new int[N]; // ๊ผญ์ง์ ์ y์ขํ๋ค
// 1. ์์์ (startIdx)(๋งจ ์ผ์ชฝ ์๋์ ์ง์ )์ ์ฐพ๊ธฐ ์ํ ํ๋
int minLeft = Integer.MAX_VALUE;
int minBottom = Integer.MAX_VALUE;
for(int i = 0; i < N; i++) {
ss = in.readLine().split(" ");
xCoordinates[i] = Integer.parseInt(ss[0]);
yCoordinates[i] = Integer.parseInt(ss[1]);
if(xCoordinates [i] <= minLeft && yCoordinates[i] <= minBottom) {
minLeft = xCoordinates[i];
minBottom = yCoordinates[i];
startIdx = i;
}
}
2. ๋ด์ฐ๋ฆฌ์ ์์์ (์ด๋ฆฐ ๊ดํธ)๊ณผ ๋์ (๋ซํ ๊ดํธ) ์ง์ ๐
ํ ๊ฐ์ ๋ด์ฐ๋ฆฌ๋ฅผ ๋ง๋ค๊ธฐ ์์ํ๋ฉด ํด๋น ๋ด์ฐ๋ฆฌ๋ฅผ ๋ฌด์กฐ๊ฑด ์์ฑํด์ผ์ง ๋ค์ ๋ด์ฐ๋ฆฌ๋ฅผ ๋ง๋ค ์ ์๋ค. (๊ทธ๋ฆผ์ ๊ทธ๋ ค๋ณด๋ฉด ์ฝ๊ฒ ๊ทธ ์ด์ ๋ฅผ ์ ์ ์๋ค.) ์ด ์ ์์ ์ฐฉ์ํ์ฌ, ๋ด์ฐ๋ฆฌ์ ์์์ ๋๋ ๋์ ์ด ๋ ์ ์๋ ์ขํ๊ฐ ๋์ค๋ฉด ์ดํ์ ๋์ค๋ ๋ด์ฐ๋ฆฌ์ ๋์ ๋๋ ์์์ ์ ๋ฌด์กฐ๊ฑด ๊ฐ์ ๋ด์ฐ๋ฆฌ์ ์ํ๊ฒ ๋๋ค.
๊ทธ๋์ pointCount๋ผ๋ ๋ณ์๋ฅผ ์ด์ฉํ์ฌ pointCount๊ฐ 2๊ฐ๊ฐ ๋๋ ๊ฒฝ์ฐ(๋ด์ฐ๋ฆฌ์ ์์์ ๊ณผ ๋์ ์ด ๋ชจ๋ ๋์จ ๊ฒฝ์ฐ)์ ์์์ (์ด๋ฆฐ ๊ดํธ)๊ณผ ๋์ (๋ซํ ๊ดํธ)์ผ๋ก ์ด๋ฃจ์ด์ง ํ๋์ ๋ด์ฐ๋ฆฌ๋ฅผ ๋ง๋ค ์ ์๋ค. ์์์ (์ด๋ฆฐ ๊ดํธ)๊ณผ ๋์ (๋ซํ ๊ดํธ)์ ๋ ์ง์ ์ x์ขํ์ ์๋์ ์ธ ์์น๋ฅผ ์ด์ฉํ์ฌ, x์ขํ๊ฐ ์์ ๊ฒฝ์ฐ์๋ ์์์ (์ด๋ฆฐ ๊ดํธ)์ ๋ถ์ฌํ๊ณ x์ขํ๊ฐ ํฐ ๊ฒฝ์ฐ์๋ ๋์ (๋ซํ ๊ดํธ)์ ๋ถ์ฌํ๋ค.
// y๋ก ํ์๋ ํ์ฌ ๊ผญ์ง์ ์ y์ขํ์ ny๋ก ํ์๋ ๋ค์ ๊ผญ์ง์ ์ y์ขํ์ ๋ถํธ๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ์
// ๋ด์ฐ๋ฆฌ์ ์์์ ๋๋ ๊ผญ์ง์ ์ด ๋ง๋ค์ด์ง๋ค.
if((y > 0 && ny < 0) || (y < 0 && ny > 0)) {
twoPointsInPeak[pointCount++] = x;
if(pointCount == 2) { // ๋ด์ฐ๋ฆฌ์ ์์์ ๊ณผ ๋์ ์ ๋ชจ๋ ์ฐพ์ ๊ฒฝ์ฐ
// 2๊ฐ์ ๋ถ๋ถ์์ ์ด๋ ๋ถ๋ถ์ด ์์์ (์ด๋ฆฐ ๊ดํธ)๋๋ ๋์ (๋ซํ ๊ดํธ)์ธ์ง๋ x์ขํ์ ์๋์ ์ธ ์์น๋ฅผ ์ด์ฉํ์ฌ ํ์
int xCoordinateAtOpenPoint = Math.min(twoPointsInPeak[0], twoPointsInPeak[1]);
int xCoordinateAtClosePoint = Math.max(twoPointsInPeak[0], twoPointsInPeak[1]);
points.add(new Point(xCoordinateAtOpenPoint, true));
points.add(new Point(xCoordinateAtClosePoint, false));
pointCount = 0;
}
}
3. ๋ด์ฐ๋ฆฌ๋ค์ ์ขํ ์ ๋ ฌ ๐ญ
๋ด์ฐ๋ฆฌ๋ค์ x์ขํ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌด์์๋ก ๋ง๋ค ์ ์๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋ชจ๋ ๋ด์ฐ๋ฆฌ๋ค์ ์์์ ๊ณผ ๋์ ์ ๋ชจ๋ ์ ์ฅํ ํ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ฃผ์ด์ผ ํ๋ค. ์ด๋ฅผ ์ํด์ ์ขํ๋ฅผ ๋ํ๋ด๋ Point ํด๋์ค์ Comparable์ ์ค์ ํด ์ฃผ๊ณ Collections.sort()๋ฅผ ์ด์ฉํ์๋ค.
// ๋ด์ฐ๋ฆฌ์ ์์์ ๋๋ ๋์ ์ ํ์
static class Point implements Comparable<Point> {
// x์ขํ
int x;
// ๋ด์ฐ๋ฆฌ์ ์์์ ์ธ ๊ฒฝ์ฐ(์ด๋ฆฐ ๊ดํธ์ธ ๊ฒฝ์ฐ) -> true
// ๋ด์ฐ๋ฆฌ์ ๋์ ์ธ ๊ฒฝ์ฐ(๋ซํ ๊ดํธ์ธ ๊ฒฝ์ฐ) -> false
boolean isOpenParenthesis;
public Point(int x, boolean isOpenParenthesis) {
super();
this.x = x;
this.isOpenParenthesis = isOpenParenthesis;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
// 3. ๋ด์ฐ๋ฆฌ๋ฅผ ์ ๋ ฌํ๋ ๊ฒ (๋ด์ฐ๋ฆฌ๋ x์ขํ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌด์์๋ก ์์ฑ๋ ์ ์๊ธฐ ๋๋ฌธ์ ์ ๋ ฌ ๊ณผ์ ์ด ํ์ํจ)
Collections.sort(points);
4. ์คํ(๊ดํธ ๋ฌธ์ )์ ์ด์ฉํ ์๊ตฌ์กฐ๊ฑด 2๊ฐ์ง ์นด์ดํธ ๐
์ด ๋ถ๋ถ์ ๋งค์ฐ ๊ฐ๋จํ๋ค. "๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ํฌํจ๋์ง ์๋ ๋ด์ฐ๋ฆฌ"๋ stack์ด ๋น์ด์๋ ์ํฉ์์ ์๋ก์ด ๋ด์ฐ๋ฆฌ๋ฅผ ๋ง๋๋ ๊ฒฝ์ฐ์ ์นด์ดํธํด ์ฃผ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ "๋ค๋ฅธ ๋ด์ฐ๋ฆฌ๋ฅผ ํฌํจํ์ง ์๋ ๋ด์ฐ๋ฆฌ ๊ฐ์"๋ ์ด๋ฆฐ ๊ดํธ์ ๋ซํ ๊ดํธ๊ฐ ๋ถ์ด ์๋ ๊ฒฝ์ฐ์ ์นด์ดํธํด ์ค๋ค.
// 4. ์๊ตฌ์กฐ๊ฑด 2๊ฐ์ง์ ๊ฐ์ ์นด์ดํธ
// (1) ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ํฌํจ๋์ง ์๋ ๋ด์ฐ๋ฆฌ (countA) = ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ํฌํจ๋์ง ์๋ ์๋ก์ด ๋ด์ฐ๋ฆฌ๊ฐ ๋ง๋ค์ด์ง๋ ๊ฒฝ์ฐ (Stack์ด ๋น์ด์๋ ์ํ์์ ์ด๋ฆฐ ๊ดํธ๊ฐ ์์๋๋ ๊ฒฝ์ฐ)
// (2) ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ๋ฅผ ํฌํจํ์ง ์๋ ๋ด์ฐ๋ฆฌ ๊ฐ์ (countB) = ๋ด์ฐ๋ฆฌ๊ฐ ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ์ํฅ์ ๋ฐ์ง ์๋ ๊ฒฝ์ฐ (์ด๋ฆฐ ๊ดํธ ๋ค์์ ๋ฐ๋ก ๋ซํ ๊ดํธ๊ฐ ๋ฐ๋ผ์ค๋ ๊ฒฝ์ฐ)
Stack<Character> stack = new Stack<>();
for(int i = 0; i < points.size(); i++) {
if(points.get(i).isOpenParenthesis) { // ์ด๋ฆฐ ๊ดํธ์ธ ๊ฒฝ์ฐ
if(stack.isEmpty()) { // (1)์ ๊ฒฝ์ฐ
countA++;
}
stack.push('(');
if(!points.get(i + 1).isOpenParenthesis) { // (2)์ ๊ฒฝ์ฐ
countB++;
}
}
else { // ๋ซํ ๊ดํธ์ธ ๊ฒฝ์ฐ
stack.pop();
}
}
์ ์ฒด์ฝ๋ ๐ฅ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
public class Main {
static StringBuilder sb = new StringBuilder();
static int N, y, x, ny, nx, countA, countB, pointCount, startIdx, hereIdx, nextIdx;
static String[] ss;
static List<Point> points = new ArrayList<>();
static int[] twoPointsInPeak = new int[2];
static int[] xCoordinates, yCoordinates;
// 0. ๋ด์ฐ๋ฆฌ์ ์์์ ๋๋ ๋์ ์ ํ์
static class Point implements Comparable<Point> {
int x;
// ๋ด์ฐ๋ฆฌ์ ์์์ ์ธ ๊ฒฝ์ฐ(์ด๋ฆฐ ๊ดํธ์ธ ๊ฒฝ์ฐ) -> true
// ๋ด์ฐ๋ฆฌ์ ๋์ ์ธ ๊ฒฝ์ฐ(๋ซํ ๊ดํธ์ธ ๊ฒฝ์ฐ) -> false
boolean isOpenParenthesis;
public Point(int x, boolean isOpenParenthesis) {
super();
this.x = x;
this.isOpenParenthesis = isOpenParenthesis;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(in.readLine());
xCoordinates = new int[N]; // ๊ผญ์ง์ ์ x์ขํ๋ค
yCoordinates = new int[N]; // ๊ผญ์ง์ ์ y์ขํ๋ค
// 1. ์์์ (startIdx)(๋งจ ์ผ์ชฝ ์๋์ ์ง์ )์ ์ฐพ๊ธฐ ์ํ ํ๋
int minLeft = Integer.MAX_VALUE;
int minBottom = Integer.MAX_VALUE;
for(int i = 0; i < N; i++) {
ss = in.readLine().split(" ");
xCoordinates[i] = Integer.parseInt(ss[0]);
yCoordinates[i] = Integer.parseInt(ss[1]);
if(xCoordinates [i] <= minLeft && yCoordinates[i] <= minBottom) {
minLeft = xCoordinates[i];
minBottom = yCoordinates[i];
startIdx = i;
}
}
// 2. ๋ด์ฐ๋ฆฌ์ ์์์ (์ด๋ฆฐ ๊ดํธ)๊ณผ ๋์ (๋ซํ ๊ดํธ)์ ์ง์
hereIdx = startIdx;
while(true) {
nextIdx = hereIdx + 1;
if(nextIdx == N) {
nextIdx = 0;
}
x = xCoordinates [hereIdx];
y = yCoordinates[hereIdx];
nx = xCoordinates [nextIdx];
ny = yCoordinates[nextIdx];
// ๋ด์ฐ๋ฆฌ์ ์์์ ๋๋ ๋์ ์ด ๋ง๋ค์ด์ง๋ ์ํฉ
if((y > 0 && ny < 0) || (y < 0 && ny > 0)) {
twoPointsInPeak[pointCount++] = x;
if(pointCount == 2) { // ๋ด์ฐ๋ฆฌ์ ์์์ ๊ณผ ๋์ ์ ๋ชจ๋ ์ฐพ์ ๊ฒฝ์ฐ
// 2๊ฐ์ ๋ถ๋ถ์์ ์ด๋ ๋ถ๋ถ์ด ์์์ (์ด๋ฆฐ ๊ดํธ)๋๋ ๋์ (๋ซํ ๊ดํธ)์ธ์ง๋ x์ขํ์ ์๋์ ์ธ ์์น๋ฅผ ์ด์ฉํ์ฌ ํ์
int xCoordinateAtOpenPoint = Math.min(twoPointsInPeak[0], twoPointsInPeak[1]);
int xCoordinateAtClosePoint = Math.max(twoPointsInPeak[0], twoPointsInPeak[1]);
points.add(new Point(xCoordinateAtOpenPoint, true));
points.add(new Point(xCoordinateAtClosePoint, false));
pointCount = 0;
}
}
hereIdx++;
if(hereIdx == N) {
hereIdx = 0;
}
if(hereIdx == startIdx)
break;
}
// 3. ๋ด์ฐ๋ฆฌ๋ฅผ ์ ๋ ฌํ๋ ๊ฒ (๋ด์ฐ๋ฆฌ๋ x์ขํ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌด์์๋ก ์์ฑ๋ ์ ์๊ธฐ ๋๋ฌธ์ ์ ๋ ฌ ๊ณผ์ ์ด ํ์ํจ)
Collections.sort(points);
// 4. ์๊ตฌ์กฐ๊ฑด 2๊ฐ์ง์ ๊ฐ์ ์นด์ดํธ
// (1) ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ํฌํจ๋์ง ์๋ ๋ด์ฐ๋ฆฌ (countA) = ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ํฌํจ๋์ง ์๋ ์๋ก์ด ๋ด์ฐ๋ฆฌ๊ฐ ๋ง๋ค์ด์ง๋ ๊ฒฝ์ฐ (Stack์ด ๋น์ด์๋ ์ํ์์ ์ด๋ฆฐ ๊ดํธ๊ฐ ์์๋๋ ๊ฒฝ์ฐ)
// (2) ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ๋ฅผ ํฌํจํ์ง ์๋ ๋ด์ฐ๋ฆฌ ๊ฐ์ (countB) = ๋ด์ฐ๋ฆฌ๊ฐ ๋ค๋ฅธ ๋ด์ฐ๋ฆฌ์ ์ํด ์ํฅ์ ๋ฐ์ง ์๋ ๊ฒฝ์ฐ (์ด๋ฆฐ ๊ดํธ ๋ค์์ ๋ฐ๋ก ๋ซํ ๊ดํธ๊ฐ ๋ฐ๋ผ์ค๋ ๊ฒฝ์ฐ)
Stack<Character> stack = new Stack<>();
for(int i = 0; i < points.size(); i++) {
if(points.get(i).isOpenParenthesis) { // ์ด๋ฆฐ ๊ดํธ์ธ ๊ฒฝ์ฐ
if(stack.isEmpty()) { // (1)์ ๊ฒฝ์ฐ
countA++;
}
stack.push('(');
if(!points.get(i + 1).isOpenParenthesis) { // (2)์ ๊ฒฝ์ฐ
countB++;
}
}
else { // ๋ซํ ๊ดํธ์ธ ๊ฒฝ์ฐ
stack.pop();
}
}
System.out.println(countA + " " + countB);
}
}
์ฃผ์์ (์๊ฐ๋ณต์ก๋) ๐ฅ
๊ผญ์ง์ ๋ค์ ์ ์ฅํ๋ ๋ฆฌ์คํธ๋ฅผ ArrayList๊ฐ ์๋ LinkedList๋ฅผ ์ฌ์ฉํ์ง ์๋๋ก ์ฃผ์ํ์.
List<Point> points = new ArrayList<>(); // O
List<Point> points = new LinkedList<>(); // X
๊ผญ์ง์ (N)์ ์ต๋ 1,000,000๊ฐ ์ฌ ์ ์๋๋ฐ ์ด๋ LinkedList๋ฅผ ์ด์ฉํด์ ์กฐํํ๋ฉด ์๊ฐ ์ด๊ณผ๊ฐ ๋๋ค. LinkedList์ ๊ฒฝ์ฐ ๋ชจ๋ ๊ผญ์ง์ (1,000,000๊ฐ)์ ์กฐํ(O(1,000,000)) ํ๋ฉด 1,000,000,000,000 => 10,000์ด๊ฐ ๊ฑธ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๋ ๊ฒ ํ๋ฉด N <= 10,000์ธ ์๋ธ ํ ์คํธ๋ง ํต๊ณผํ์ฌ '100์ '์ด ์๋ '๋ถ๋ถ ์ ์'๋ง ๋ฐ๊ฒ ๋๋ค.