Initially, I thought of using Next Greater to Left & Next Greater to Right concepts, & did dry run as per below written code for the test cases, [1,2,2], [3,2,1,2,1,7] & [4,4,4,4,4]. According to the dry run, I got the correct answers, but when I run the test cases on leetcode, I get correct output for [1,2,2], but for [3,2,1,2,1,7] & [4,4,4,4,4], it shows output of 4, which is wrong. My dry run (as per the same code) yielded correct answer, but on running on platform, it fails. Can anyone help me address this issue ? The code is : class Solution { private: vector NGR(vector& arr, int n){ stack st; vector result(n, -1); for (int i = n - 1; i >= 0; i--) { while (!st.empty() && st.top()
Really good explanation!
amazing explanation
In line 12 of your code, how's it guaranteed that newValue will be at max only one less than nums[i-1]?
Becuz we have sorted array & increment nums(i-1) value that's why nums(i-1) is greater than or equal current nums(I) value 😊
Initially, I thought of using Next Greater to Left & Next Greater to Right concepts, & did dry run as per below written code for the test cases, [1,2,2], [3,2,1,2,1,7] & [4,4,4,4,4]. According to the dry run, I got the correct answers, but when I run the test cases on leetcode, I get correct output for [1,2,2], but for [3,2,1,2,1,7] & [4,4,4,4,4], it shows output of 4, which is wrong. My dry run (as per the same code) yielded correct answer, but on running on platform, it fails. Can anyone help me address this issue ? The code is :
class Solution {
private:
vector NGR(vector& arr, int n){
stack st;
vector result(n, -1);
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && st.top()
Wrong solution