[백준]_11501_주식

11501.png

`stack 이용 문제


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class B2841 {
	static int N,P,num,line, count = 0;
	static Stack<Integer>[] arr = new Stack[6];
	// 6개의 stack 배열 
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		P = Integer.parseInt(st.nextToken());
		for(int i=0;i<6;i++) {
			arr[i] = new Stack<>();
		}
		while(N-->0) {
			st = new StringTokenizer(br.readLine());
			line = Integer.parseInt(st.nextToken());
			num = Integer.parseInt(st.nextToken());
			while(true) {
				if(arr[line].isEmpty()) {
					arr[line].push(num);
					count++;
					break;
				}

				// 비어 있지 않은 경우
				int peek = arr[line].peek();

				// 값이 더 클 경우 pop
				if(peek > num) {
					arr[line].pop();
					count++;
				}
				// 값이 작을 경우 새로운 값 넣고 종료
				else if(peek<num){
					arr[line].push(num);
					count++;
					break;
				// 같을 경우 종료
				}else
					break;
			}
		}
		System.out.println(count);
	}
}
updatedupdated2021-02-282021-02-28
Load Comments?