forked from abhaygupta08/Hacktober-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
975-ODD EVEN JUMP SOLUTION.txt
91 lines (59 loc) · 1.84 KB
/
975-ODD EVEN JUMP SOLUTION.txt
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
class Solution {
public:
int fun(vector<int>&a,vector<int>&b,int i,int k,vector<vector<int>>&dp)
{
if(i==a.size()-1)return 1;
if(dp[i][k]!=-1)return dp[i][k];
long int res=0;
if(k&1&&a[i]!=-1)res|=fun(a,b,a[i],(k+1)%2,dp);
if(!(k&1)&&b[i]!=-1)res|=fun(a,b,b[i],(k+1)%2,dp);
return dp[i][k]=res;
}
int oddEvenJumps(vector<int>& arr1) {
int n=arr1.size();
set<pair<int,int>>s;vector<pair<int,int>>arr;
for(int i=0;i<n;i++)arr.push_back(make_pair(arr1[i],i));
vector<int>v;
for(int i=n-1;i>=0;i--)
{
auto it=s.lower_bound(arr[i]);
s.insert(arr[i]);
if(it==s.end())
{
v.push_back(-1);
}
else
{
pair<int,int>a=*it;
v.push_back(a.second);
}
}
reverse(v.begin(),v.end());
vector<int>v1;set<int>s1;map<int,int>count;
for(int i=n-1;i>=0;i--)
{
if(i==n-1){s1.insert(arr[i].first);count[arr[i].first]=i;v1.push_back(-1);continue;}
auto it=s1.upper_bound(arr[i].first);
// cout<<count[*it];
if(it==s1.begin())
{
v1.push_back(-1);
}
else
{it--;
// cout<<*it<<" ";
v1.push_back(count[*it]);
}
s1.insert(arr[i].first);
count[arr[i].first]=i;
}
reverse(v1.begin(),v1.end());
//for(int i=0;i<n;i++)cout<<v1[i]<<" ";
vector<vector<int>>dp(arr1.size()+1,vector<int>(3,-1));
long int ans=0;
for(int i=0;i<arr1.size();i++)
ans+=fun(v,v1,i,1,dp);
return ans;
return 0;
}
};