Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Solution::maxSpecialProduct(vector<int> &A) {
- int n=A.size();
- vector<int> B(n,-1),C(n,-1);
- stack<int> s;
- for(int i=0;i<n;i++)
- {
- while(!s.empty()&&A[i]>=A[s.top()]){
- s.pop();
- }
- if(!s.empty())
- B[i]=s.top();
- s.push(i);
- }
- s={};
- for(int i=n-1;i>=0;i--)
- {
- while(!s.empty()&&A[i]>=A[s.top()]){
- s.pop();
- }
- if(!s.empty())
- C[i]=s.top();
- s.push(i);
- }
- long long ans=0;
- long long mod=(1e9+7);
- for(int i=0;i<n;i++)
- {
- B[i]=max(B[i],0);
- C[i]=max(C[i],0);
- ans=max(ans,(B[i]*C[i]*1LL));
- }
- ans%=mod;
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement