풀이
스택을 사용하여 해결했습니다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static class Info {
int idx, height;
public Info(int idx, int height) {
super();
this.idx = idx;
this.height = height;
}
}
public static int N;
public static int[] tower;
public static int[] answer;
public static Stack<Info> stk;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder("");
StringTokenizer st = null;
N = Integer.parseInt(br.readLine());
tower = new int[N + 1];
answer = new int[N + 1];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i <= N; ++i) {
tower[i] = Integer.parseInt(st.nextToken());
}
int cur = N;
stk = new Stack<Info>();
do {
while (!stk.isEmpty() && stk.peek().height <= tower[cur]) {
Info info = stk.pop();
answer[info.idx] = cur;
}
stk.add(new Info(cur, tower[cur--]));
} while (cur > 0);
for (int i = 1; i <= N; ++i) {
sb.append(answer[i]).append(" ");
}
System.out.println(sb);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] BOJ 9252번 LCS2 (0) | 2021.06.25 |
---|---|
[Java] BOJ 9019번 DSLR (0) | 2021.06.24 |
[Java] BOJ 16973번 직사각형 탈출 (0) | 2021.06.22 |
[Java] BOJ 3109번 빵집 (0) | 2021.06.19 |
[Java] BOJ 5014번 스타트링크 (0) | 2021.06.18 |