Those who is getting Memory Limit Exceeded just do ans += st.top(); And reverse the ans string after execution of the loop. reverse(ans.begin(),ans.end()) By the way Awesome Explanation as always.
@@ashady8 Ya, when you do ans = st.top() + ans it creates copy of both st.top() and ans, since st.top() is a character, so, it does not create much effect on memory but ans is string so when ans is very large then it gives you Memory Limit Exceeded but when you do ans += st.top() it creates copy of only st.top() so we reduced memory usage but it is in reverse form so you have to reverse the array after the loop.
Dear Ayushi, While implementing your logic, It is showing memory limit exceeded while submitting, Kindly look into it. I think the while loop check for stack not empty && top element > num[i] && k>0 is causing this issue!
Amazing video! I can say this explanation is one of the best of yours 🤩🤩 Please explain like this only from now onwards and one more suggestion please don't care about length of video u just make the tutorial like this coz explanation like this is much needed and which is missing On RU-vid
THIS SOLUTION IS GIVING MEMORY LIMIT EXCEED ON 42nd TESTCASE to avoid that we have to declare an empty string and then use push_back operation to fill it and then use reverse // this problem wasted 30 min , please correct your code
Although i didn't see your entire video because i got the hint from the title. but i checked your description box and you really have amazing playlist.
I guess we dont even need a stack this can be done in place that will be optimal string removeKdigits(std::string num, int k) { int n = num.length(); int top = -1; for (int i = 0; i < n; i++) { while (k > 0 && top >= 0 && num[i] < num[top]) { top--; k--; } num[++top] = num[i]; } while (k > 0 && top >= 0) { top--; k--; } int leadingZeros = 0; while (leadingZeros