백준 - 6198번 옥상 정원 꾸미기 (골드 5)
2024. 3. 8. 19:06ㆍ코딩테스트 정리(자바)
728x90
시간초과......
다른 방법은 떠오르지 않는다...
import java.io.*;
import java.util.*;
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[] heights=new int[N];
Stack <Integer> stack1=new Stack<>();
Stack <Integer> stack2=new Stack<>();
for(int i=0;i<N;i++){
heights[i]=Integer.parseInt(br.readLine());
}
for(int i=N-1;i>=0;i--){
stack1.push(heights[i]);
}
int height=0;
int sum=0;
while(!stack1.isEmpty()){
height=stack1.pop();
while(!stack1.isEmpty()&& height>stack1.peek()){
stack2.push(stack1.pop());
}
sum+=stack2.size();
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
}
bw.write(Integer.toString(sum));
bw.flush();
bw.close();
}
}728x90
'코딩테스트 정리(자바)' 카테고리의 다른 글
| 백준 - 큐2 (18258번) 실버 4 (0) | 2024.03.11 |
|---|---|
| 백준 - 6198번 옥상 정원 꾸미기 (0) | 2024.03.09 |
| 백준 - 2493번 탑 (골드 5) (0) | 2024.03.07 |
| 백준 - 스택 수열 (실버 2) (0) | 2024.03.06 |
| 백준 - 요세푸스 문제 (실버 4) (0) | 2024.03.06 |