only two test cases pass,
Find the greatest eleement
can you help me modify this code, i am not able to figure it out
if u double the array and then apply the same logic
it will work fine. becuase doubling the array will compensate the circular property.
refer this->
vector<int> nextGreaterElements(vector<int> &nums)
{
int n = nums.size();
vector<int> res(n);
stack<int> s;
for (int i = 2 * n - 1; i >= 0; i--)
{
while (!s.empty() && nums[s.top()] <= nums[i % n])
{
s.pop();
}
res[i % n] = (s.empty() ? -1 : nums[s.top()]);
s.push(i % n);
}
return res;
}
didnt quite get the approach of this code but still tried to modified it accordingly.
I know its wrong but still can you help me identify my errors and tell me where i am going wrong and also explain the logic behind it for circular array
check now->
to understand the logic,
assume this array as circular [1,2,3] and then iterate it clockwise 2 times and see what u get.
u will get [1,2,3,1,2,3] which is nothing but doubling the given array.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.