이게 왜 실버3인지 모르겠다..(어렵다는뜻..)
https://www.acmicpc.net/problem/12789
문제풀이
코드
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int N=Integer.parseInt(br.readLine());
int[] line=new int[N];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<N;i++) {
line[i]=Integer.parseInt(st.nextToken());
}
bw.write(solve(line));
bw.flush(); bw.close();
}
public static String solve(int[] line) {
int orderValue=1;
Stack<Integer> tmp=new Stack<>();
for(int i=0;i<line.length;i++) {
if(line[i]==orderValue) {orderValue++;}
else {
if(!tmp.isEmpty()&&tmp.peek()==orderValue) {
tmp.pop();
i--; // line에서 얻은 값이 아니고 tmp(임시 저장소에서 얻은 값이어서)
orderValue++;
}else {
tmp.push(line[i]);
}
}
}
boolean result=true;
// 임시 저장소에 값이 남아있을 경우 탐색
while(!tmp.isEmpty()) {
if(tmp.peek()==orderValue) {
tmp.pop();
orderValue++;
}else {
result=false;
break;
}
}
return result ? "Nice":"Sad";
}
}
결과
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준_18870/JAVA] 좌표 압축 (0) | 2025.02.27 |
---|---|
[백준_2346/JAVA] 풍선 터트리기 (0) | 2025.02.06 |
[백준_11286/JAVA] 절댓값 힙 (1) | 2025.01.07 |
[백준_2164/JAVA] 카드 게임_스택, 큐 풀이 (0) | 2025.01.05 |
[백준_1874/JAVA] 스택으로 오름차순 수열 만들기 (3) | 2025.01.04 |