Ninja technique🥷 to ACE DSA Interviews.
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n,-1);
for(int i = 0;i< n; i++){
for(int j = 0; j < n ;j++){
if(nums[i]<nums[(j+i)%n]){
ans[i] = nums[(j+i)%n];
break;
}
}
}
return ans;
}
};
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums)
{
vector<int> ans(nums.size(),-1);
stack<int> st;
int n = nums.size();
for(int i = 0; i< 2*n ; i++){
while(!st.empty() && nums[st.top()]<nums[i%n]){
ans[st.top()] = nums[i%n];
st.pop();
}
st.push(i%n);
}
return ans;
}
};