LeetCode Next Greater Element I Solution Explained - Java

  Рет қаралды 33,877

Nick White

Nick White

4 жыл бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow Me on X/Twitter - x.com/nickwhitereal
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 39
@LuisMorales-yx8di
@LuisMorales-yx8di 2 жыл бұрын
It would be cool if you could discuss time and space complexity.
@kdkwarteng7610
@kdkwarteng7610 4 жыл бұрын
Could you possibly discuss runtime as you go through these
@LuisMorales-yx8di
@LuisMorales-yx8di 2 жыл бұрын
+1
@namansharma5128
@namansharma5128 2 жыл бұрын
@@LuisMorales-yx8di +1000 -1000
@chaoluncai1607
@chaoluncai1607 2 жыл бұрын
as the worst case scenario is stack push and pop each element in nums2[] once, and we have to loop through every element in nums1[] for the result, hence I think it's O(2*nums2.length + nums1.length) = O(nums1.length+nums2.length)
@MohitSinha4
@MohitSinha4 4 жыл бұрын
Your explanation is just wow!
@alind_singh
@alind_singh 5 ай бұрын
Can anyone help me with the time complexity of the nested loops in the given solution?
@amrholo4445
@amrholo4445 2 жыл бұрын
Thanks a lot, sir may you discuss the time and space complexity please, I find it hard to come up with it myself
@zihaosong1514
@zihaosong1514 3 жыл бұрын
Thanks for your explanation!
@rahuldabas8233
@rahuldabas8233 4 жыл бұрын
great explanation dude!!
@darod6098
@darod6098 4 жыл бұрын
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
@chishikiendeavourer8663
@chishikiendeavourer8663 3 жыл бұрын
public static int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] nextGreaterElements = new int[nums1.length]; int nextGreaterElement = -1; for (int k = 0; k < nums1.length; ++k) { nextGreaterElement = -1; for (int i = 0; i < nums2.length; ++i) { if (nums2[i] == nums1[k]) { for (int j = i + 1; j < nums2.length; ++j) { if (nums2[j] > nums1[k]) { nextGreaterElement = nums2[j]; break; } } break; } } nextGreaterElements[k] = nextGreaterElement; } return nextGreaterElements; }
@bisujin1685
@bisujin1685 4 жыл бұрын
Very good explanation thanks
@srinadhp
@srinadhp 2 жыл бұрын
great explanation! Thank yoU!
@vivekshokeen1192
@vivekshokeen1192 2 жыл бұрын
We should traverse from the backside of the nums2 array for more intuitive approach
@syedmehdiabbas1588
@syedmehdiabbas1588 3 жыл бұрын
I love your humor Nick
@chodingninjas7415
@chodingninjas7415 4 жыл бұрын
great explanation
@lifeofme3172
@lifeofme3172 4 жыл бұрын
I don't like hit me up 😂 good explanation
@aravindhravi2307
@aravindhravi2307 4 жыл бұрын
This is beautiful
@anupamsingh8925
@anupamsingh8925 4 жыл бұрын
sir how to get this level approach for doing such types of question?
@ronaldo7rik
@ronaldo7rik 4 жыл бұрын
Practice
@amaan0
@amaan0 2 жыл бұрын
Brooooo! Awesome explaination .❤
@yusselrosario5208
@yusselrosario5208 7 ай бұрын
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;
@quirkyquester
@quirkyquester 3 жыл бұрын
amazing explanation! Thank you!
@VinayKumar-xs6el
@VinayKumar-xs6el Ай бұрын
You are just a Legend
@MeetManga
@MeetManga 2 жыл бұрын
there is a bug : if there are some duplicted numbers in the array, then the value of key in the hasMap will be overwrited.
@frankf5398
@frankf5398 Жыл бұрын
It's guranteed that there's no duplicates
@shubhammishra6119
@shubhammishra6119 3 жыл бұрын
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.
@shubhammishra6119
@shubhammishra6119 3 жыл бұрын
Instead of Peek you should stack last element
@rafishaik2496
@rafishaik2496 2 жыл бұрын
explain nge2 leet code solution
@harshithahprabhu6220
@harshithahprabhu6220 4 жыл бұрын
what's the time complexity?
@iamnoob7593
@iamnoob7593 4 жыл бұрын
O(nums1.length + nums2.length) ~ O(m+n)
@sumansumann8947
@sumansumann8947 4 жыл бұрын
@@iamnoob7593 as there is a while loop inside the first for loop ,, how it would be linear time complexity??
@iamnoob7593
@iamnoob7593 4 жыл бұрын
@@sumansumann8947 worst case if u keep inserting elements in stack. After inserting last element in stack.. for is done no more.... It's linear. Analyze on paper.
@user-hl2sk1ow7u
@user-hl2sk1ow7u 3 жыл бұрын
Your runtime complexity here is O(N^2 ), right?
@alaaeldin-khaled
@alaaeldin-khaled 2 жыл бұрын
nope. watch Abdul Bari videos about time complexity on youtube
@pranshusati5115
@pranshusati5115 10 ай бұрын
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 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; }
@whoami8247
@whoami8247 10 ай бұрын
Why did you reverse the array
@pranshusati5115
@pranshusati5115 10 ай бұрын
@@whoami8247 that line has been commented plz look closely
LeetCode Reorder List Solution Explained - Java
12:55
Nick White
Рет қаралды 31 М.
LeetCode 735. Asteroid Collision (Solution Explained)
14:24
Nick White
Рет қаралды 20 М.
UNO!
00:18
БРУНО
Рет қаралды 3,3 МЛН
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 9 МЛН
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 30 МЛН
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 245 М.
Next Greater Element I - Leetcode 496 - Python
14:53
NeetCode
Рет қаралды 69 М.
Google Coding Interview With A Facebook Software Engineer
49:59
Clément Mihailescu
Рет қаралды 929 М.
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 96 М.
All Software Developers NEED a Portfolio
14:57
Nick White
Рет қаралды 43 М.
LeetCode 238. Product of Array Except Self (Solution Explained)
14:49
LeetCode 442. Find All Duplicates in an Array (Solution Explained)
12:37
Хакер взломал компьютер с USB кабеля. Кевин Митник.
0:58
Последний Оплот Безопасности
Рет қаралды 2,3 МЛН
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 7 МЛН
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 9 МЛН