
모든 스택을 다 검사하게 되는데 스택에서 가장 큰 수를 알고 있다면. 그 수를 만나자 마자 BackTracking으로 가지를 쳐버리면 더 빨리 해결할 수 있다고 생각한다.
import java.util.Scanner;
import java.util.Stack;
public class bj17608 {
static Scanner scanner = new Scanner(System.in);
static Stack<Integer> bar = new Stack<>();
public static void main(String[] args) {
inputData();
System.out.println(findAnswer());
scanner.close();
}
public static void inputData(){
System.out.println("inputData()");
int N, i, temp;
N = scanner.nextInt();
for(i = 0; i < N; i++){
temp = scanner.nextInt();
bar.push(temp);
}
}
public static int findAnswer(){
System.out.println("findAnswer()");
int answer = 1;
int max;
max = bar.pop();
while(!bar.isEmpty()){
System.out.println("현재 막대 : " + max + " / 다음 막대 : " + bar.peek());
if(bar.peek() > max){
max = bar.peek();
answer++;
}
bar.pop();
System.out.println("answer : " + answer);
}
return answer;
}
}