해커랭크(HackerRank)

Maximum Element

cepiloth 2018. 8. 19. 17:21
반응형


1. 문제


2. 알고리즘

키워드 - 구현, 스택


3. 코드


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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
 
 
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    
    stack<long int> s, m;
    m.push(-1);
    int n; cin>>n;
    int  i = 0;
    while(i<n){
        int x;
        cin>>x;
        if(x==1) {
            int z ;cin>>z;
            s.push(z);
            
            if(z >= m.top())
                m.push(z);
        }
        else if(x==2){
            int q = s.top();
            if(q == m.top())
                m.pop();
            s.pop();
        }
        else if(x==3){
            cout << m.top() << endl;
            //m.pop();
        }
        i++;
    }
    return 0;
}
cs

반응형