Wednesday, May 18, 2016

HackerRank Training DS 4

Dynamic maximum element query in O(N) using two stacks.
Let's say you are given N integers initially then you are given Q queries.
In each query , you are either told to add X to the list or remove the last added element and in the other type of query you are told to print the maximum element in the list.
You can solve it using following algorithm -  >


 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
52
53
54
55
56
57
58
59
60
61
62
63
occurances[] < - 0

 stack elementSimulation , maxElem ;

curMax = -INF

 

in < - new input 

if ( add query )

{

    elementSimulation.push(in);

    occurances[in]++;

    if( in > curMax)  // Not >= as I will only keep distinct element in maxElement stack

    {

        curMax = in;

        maxElem.push(in);

    }

 

 

} 


if( remove query )

{

elementSimulation.pop();

occurances[in]--;

if( in == curMax && occurances[in]==0) // I dont have any more copy of this max element in stack

{

    maxElem.pop();

    curMax = maxElem.top(); //The next top element will be now new max

}

if( print query)

{

   print curMax;

}


}





The idea behind adding elements in the second stack (maxElem) is that , lets say you have curMax = A1. Now you are told to add , A2 . and if (A2>A1) you add that to maxElem as it is the new max as it is > curMax . But if it is lower than A1 we dont add it to the list .
Lets say our maxElem : A4 A3 A1    and A2<=A1 but A2 > A3 .
Then , I remove an element .Look as I have added A2 later than A1,A3,A4.So,If I must remove A2 before any of these A2 can never in future be maximum.So,it is okay that we didn't add A2 to the maxElem meaning if some Ai has not the potential to be the curMax now it will never be the maximum in future as I will remove (If I have the remove query) Ai before the curMax , second curMax and so on as Ai was added last and in stack who is added last gets removed first.

 ID


Code : -




  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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
stack<ll>element_simulation;
stack<ll>max_elem;
map<ll,int>occurances;
int main()
{


    ll cur_max = -INF;
    int N;
    sfi(N);
    while(N--)
    {
        int qtype;
        sfi(qtype);
        if(qtype==1)
        {
            ll add;
            sfl(add);
            element_simulation.push(add);
            occurances[add]++;
            if(add>cur_max)
            {
                cur_max = add;
                max_elem.push(add);

            }

        }
        else if(qtype==2)
        {
            ll top_elem = element_simulation.top();
            if(!element_simulation.empty())
            element_simulation.pop();
            occurances[top_elem]--;
            if(top_elem==cur_max && occurances[top_elem]==0)
            {
                ll new_max;
                if(!max_elem.empty())
                max_elem.pop();
                if(!max_elem.empty())
                 new_max = max_elem.top();
                else
                 new_max = -INF;
                cur_max = new_max;
            }

        }
        else
        {
            pf("%lld\n",cur_max);
        }

    }

    return 0;
}


Another simplification could be avoiding the use of occurances if we changed the line 86 to
if(add>=cur_max) allowing duplicate elements in maxElem then that would be 
handled internally . We wouldn't need to worry about that.
 
 
  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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
stack<ll>element_simulation;
stack<ll>max_elem;

int main()
{


    ll cur_max = -INF;
    int N;
    sfi(N);
    while(N--)
    {
        int qtype;
        sfi(qtype);
        if(qtype==1)
        {
            ll add;
            sfl(add);
            element_simulation.push(add);
            
            if(add>=cur_max)
            {
                cur_max = add;
                max_elem.push(add);

            }

        }
        else if(qtype==2)
        {
            ll top_elem = element_simulation.top();
            if(!element_simulation.empty())
            element_simulation.pop();
            
            if(top_elem==cur_max)
            {
                ll new_max;
                if(!max_elem.empty())
                max_elem.pop();
                if(!max_elem.empty())
                 new_max = max_elem.top();
                else
                 new_max = -INF;
                cur_max = new_max;
            }

        }
        else
        {
            pf("%lld\n",cur_max);
        }

    }

    return 0;
}

No comments:

Post a Comment