LeetCode Next Greater Element I Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

It would be cool if you could discuss time and space complexity.

LuisMorales-yxdi
Автор

Could you possibly discuss runtime as you go through these

kdkwarteng
Автор

sir how to get this level approach for doing such types of question?

anupamsingh
Автор

I thought that i could do it without the stack, going through the array from 1 to n-1 but with the decreasing case at the end you made me realize that i can't.


Thank you

darod
Автор

Can anyone help me with the time complexity of the nested loops in the given solution?

alind_singh
Автор

We should traverse from the backside of the nums2 array for more intuitive approach

vivekshokeen
Автор

I don't like hit me up 😂 good explanation

lifeofme
Автор

Thanks a lot, sir
may you discuss the time and space complexity please, I find it hard to come up with it myself

amrholo
Автор

It is a bad idea to modify nums1. If a function returns an array you don't expect the input array to be modified. It also makes the code less readable;

yusselrosario
Автор

there is a bug : if there are some duplicted numbers in the array, then the value of key in the hasMap will be overwrited.

MeetManga
Автор

what if array is ar1=[1, 2, 3, 4] ar2=[1, 4, 2, 3]
stack [1]-> 4 comes , hashmap becomes ->(1, 4)
stack[4]-> 2 comes, False
stack[4, 2]-> 3 comes False

Final answer -> [4, -1, -, 1, -1]
expected -[4, 3, -1, -1]

for this condition your code is not working?
let me know if i am understanding your code wrong-> else solution.

shubhammishra
Автор

Your runtime complexity here is O(N^2 ), right?

АдемаЕргара
Автор

Hey Nick I know its 4 years sice the upload can you still help me solve this can't figure out whats the mistake in this
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> mp = new HashMap<>();
int p = -1;
// int[] reversedNums2 = Arrays.copyOf(nums2, nums2.length);
// reverseArray(reversedNums2);
mp.put(nums2[nums2.length -1], -1);
int max = nums2[nums2.length-1];
for(int i = nums2.length-2; i>=0; i--){
int current= nums2[i];
if(current < max){
if(i!=nums2.length && nums2[i+1] > current) max = nums2[i+1];
mp.put(current, max );

}
else mp.put(current, -1);

max = Math.max(current, max);

}
int[] res = new int [nums1.length];
for(int i =0; i< nums1.length; i++){
res[i] = mp.get(nums1[i]);
}
return res;
}

pranshusati
welcome to shbcf.ru