~๋ชฉ์ฐจ~
๋ฌธ์
https://www.acmicpc.net/problem/2493
๋ฌธ์ ํด๊ฒฐ ํฌ์ธํธ
- Stack์ ์ด์ฉํด ๋จผ์ ์์ธ ๊ฐ ์ค (๊ฐ์ฅ ๊ฐ๊น์ด) ๋ณธ์ธ๋ณด๋ค ํฐ ๊ฐ ์ฐพ๊ธฐ
1. Stack์ด ๋น์ด ์๋์ง ์๋์ง ํ๋ณ
2. ๋น์ด์๋ค๋ฉด 0 ์ถ๋ ฅ & ๋ณธ์ธ push VS ๋น์ด์์ง ์๋ค๋ฉด Stack์์ ๋ณธ์ธ๋ณด๋ค ํฐ ๊ฐ์ ์ฐพ์ ๋๊น์ง pop
3. ๋ณธ์ธ๋ณด๋ค ํฐ ๊ฐ์ ์ฐพ์๋ค๋ฉด ์ถ๋ ฅํ๊ณ ๋ณธ์ธ push
4. 1 ~ 3 ๋ฐ๋ณต
์์ฑ ์ฝ๋
import java.io.*;
import java.util.*;
public class ํ2493 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
Stack<int[]> stack = new Stack<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
int num = Integer.parseInt(st.nextToken());
if(stack.isEmpty()) {
sb.append("0 ");
stack.push(new int[] {i+1, num});
} else {
while(true) {
if(stack.isEmpty()) {
sb.append("0 ");
stack.push(new int[] {i+1, num});
break;
} else {
if(stack.peek()[1] > num) {
sb.append(stack.peek()[0] + " ");
stack.push(new int[] {i+1, num});
break;
} else{
stack.pop();
}
}
}
}
}
System.out.println(sb);
}
}
๋์์ด ๋์ จ๋ค๋ฉด ์ข์์ ๋๋ฌ์ฃผ์ธ์๐
'๐๋ฌธ์ ํ์ด > ๐งฉBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฐฑ์ค 16926 ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 1 | ๊ตฌํ (0) | 2023.08.09 |
---|---|
[JAVA] ๋ฐฑ์ค 12891 DNA ๋น๋ฐ๋ฒํธ | ์ฌ๋ผ์ด๋ฉ ์๋์ฐ (0) | 2023.08.04 |
[JAVA] ๋ฐฑ์ค 2961 ๋์์ด๊ฐ ๋ง๋ ๋ง์๋ ์์ (0) | 2023.08.04 |
[Python] ๋ฐฑ์ค 1654 ๋์ ์๋ฅด๊ธฐ | ์ด์ง ํ์ | ์๊ฐ ์ด๊ณผ (0) | 2023.07.08 |
[Python] ๋ฐฑ์ค 9655 ๋ ๊ฒ์ | DP (0) | 2023.06.18 |