Skip to content

Commit 3db4ef8

Browse files
committed
Day22 streak
1 parent 05bd1a0 commit 3db4ef8

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Interview Bit/Arrays/Max Distance.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
int Solution::maximumGap(const vector<int> &A) {
2+
int n = A.size();
3+
if(n==1)
4+
return 0;
5+
vector<pair<int, int>> ans;
6+
7+
//vector pair is made so that we dont lose original index after sorting of array
8+
//make vector pair to store key and value
9+
for(int i=0;i<n;i++){
10+
ans.push_back(make_pair(A[i], i));
11+
}
12+
13+
//sort vector pair based on value
14+
sort(ans.begin(), ans.end());
15+
16+
//create array to find max index from i to n
17+
int max_index[n];
18+
int mx = INT_MIN;
19+
for(int i=n-1;i>=0;i--){
20+
mx = max(mx, ans[i].second);
21+
max_index[i] = mx;
22+
}
23+
24+
//iterate vector pair and find max index from i to n
25+
int mil_gaya = INT_MIN;
26+
for(int i=0;i<n-1;i++){
27+
mil_gaya = max(mil_gaya, max_index[i] - ans[i].second);
28+
}
29+
return mil_gaya;
30+
}
31+
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
stack<int> s;
2+
stack<int> ss;
3+
MinStack::MinStack() {
4+
5+
while(!s.empty())
6+
s.pop();
7+
8+
while(!ss.empty())
9+
ss.pop();
10+
11+
}
12+
13+
void MinStack::push(int x) {
14+
s.push(x);
15+
if(ss.empty()||ss.top()>x)
16+
ss.push(x);
17+
}
18+
19+
void MinStack::pop() {
20+
21+
if(s.empty())
22+
return;
23+
24+
if(s.top()==ss.top())
25+
{
26+
s.pop();
27+
ss.pop();
28+
}
29+
else
30+
s.pop();
31+
}
32+
33+
int MinStack::top() {
34+
35+
if(s.empty())
36+
return -1;
37+
38+
return s.top();
39+
}
40+
41+
int MinStack::getMin() {
42+
43+
if(ss.empty())
44+
return -1;
45+
46+
return ss.top();
47+
}
48+
49+

0 commit comments

Comments
 (0)