Тёмный

Maximum Width Ramp | Brute Force | Better | Optimal | Leetcode 962 | codestorywithMIK 

codestorywithMIK
Подписаться 69 тыс.
Просмотров 1,2 тыс.
50% 1

Whatsapp Community Link : www.whatsapp.c...
This is the 8th Video of our Playlist "Two Pointers : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good 2 Pointer based Problem : Maximum Width Ramp | Brute Force | Better | Optimal | Leetcode 962 | codestorywithMIK
NOTE : This problem is an ideal candidate for "Monotonic Stack" but as you all know, there is some medical urgency at my home because of which I couldn't get much time today. Hence posting 2 Pointer approach only. But as soon as I get time, I am going to post a detailed explanation on Monotonic Stack approach.
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Maximum Width Ramp | Brute Force | Better | Optimal | Leetcode 962 | codestorywithMIK
Company Tags : Google, Amazon
My solutions on Github(C++ & JAVA) - github.com/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZ...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Brute Force
Description: This approach uses a nested loop to check every possible pair (i, j) where i less than j and nums[i] less than equal nums[j]. If a valid pair is found, it updates the maximum ramp width (j - i).
Time Complexity: O(n2), since it involves two loops iterating through the array.
Space Complexity: O(1), as no additional data structures are used.
Limitations: Inefficient for large inputs and fails some test cases due to time limits.
Test Case Success: Passes 95/101 test cases.
Approach 2: Early Termination
Description: Similar to the brute-force approach but optimizes by starting the inner loop from the end (j = n - 1) and breaking early once a valid ramp is found for a given i. This avoids unnecessary comparisons for each i.
Time Complexity: O(n2) in the worst case, but performs better in practice due to early termination.
Space Complexity: O(1), as it does not require extra space.
Limitations: Somewhat more efficient than Approach 1 but still can fail on large inputs.
Test Case Success: Passes 97/101 test cases.
Approach 3: Two-Pointer with Preprocessing (maxRight Array)
Description: This approach preprocesses the array by creating a maxRight array, where maxRight[i] stores the maximum element from index i to the end. Then, two pointers (i and j) are used to find the maximum width ramp. If nums[i] is less than or equal to maxRight[j], update ramp and move the j pointer right. Otherwise, increment i.
Time Complexity: O(n), as it involves a linear scan to build maxRight and another linear scan using two pointers.
Space Complexity: O(n), because of the additional maxRight array.
Advantages: Efficient and handles all test cases.
Test Case Success: Passes all test cases and is the Accepted solution.
Overall, Approach 3 is the optimal solution due to its linear time complexity and ability to handle large inputs without exceeding time limits.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment

Опубликовано:

 

9 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@codestorywithMIK
@codestorywithMIK 2 часа назад
NOTE : This problem is an ideal candidate for "Monotonic Stack" but as you all know, there is some medical urgency at my home because of which I couldn't get much time today. Hence posting 2 Pointer approach only. But as soon as I get time, I am going to post a detailed explanation on Monotonic Stack approach.
@rahilkhera8215
@rahilkhera8215 40 минут назад
Take your time. Best wishes for you and your family.
@gui-codes
@gui-codes Час назад
Salute hai is dedication ko. Take care of your family.
@ugcwithaddi
@ugcwithaddi Час назад
Praying for your family. Hats off to your dedication 🙏🏻
@abhishekkumar-fe8lw
@abhishekkumar-fe8lw 54 минуты назад
I hope the surgery goes well,and your familiy member recovers soon
@11csepratikshaargulewar71
@11csepratikshaargulewar71 2 часа назад
By the mercy of god , i hope surgery goes well sir .
@Its_Shubham_Negi
@Its_Shubham_Negi 2 часа назад
Stay Strong bhaiya !!.... Hope your family member stay safe and recover soon
@MohammedHasmi577
@MohammedHasmi577 2 часа назад
Sir literally 3 4 baar channel visit kiya even though solve que by myself but aap bahot hi acha smjate ho Edit: sir reply de do linkedIn me dete ho aesa plzz sir big fan of you❤🎉
@PIYUSHRAJ-t5v
@PIYUSHRAJ-t5v Час назад
Just want to point one catch: Your solution of O(N^2) can be easily changed to O(N) by changing the second “while” by “if”, as we simply need to increase i pointer by 1 step(not always) but,the test cases are designed such that no next nums[i]>maxiFromRight[j], so this minor change works. To make it work for any test cases if added in future, just add one if statement before res=max(res,j-i); , which ultimately represents the same thing as your code. Also, the check of i=0;i--){ maxi=max(maxi,nums[i]); maxiFromRight[i]=maxi; } int res=0; int i=0,j=1; while(jmaxiFromRight[j])i++; res=max(res,j-i); j++; } return res; } };
@thekindspill
@thekindspill 58 минут назад
Better than Neetcode explanation to this same approach. I liked how you related the Approach-2 to 2 pointer approach. The intuition clicked and I solved it easily. Best wishes to your family ❤
@salmaniproductions1104
@salmaniproductions1104 26 минут назад
Thank you so much bhaiya, Hope the sugery of your family member go well, Take care of yourself and your family bhaiyya
@itskaaryan7
@itskaaryan7 2 часа назад
Everything will be good bhaiya, we all pray for speedy recovery. ✨
@Anshul-qb2pm
@Anshul-qb2pm Час назад
praying for your family member. stay strong
@ayushkumarsingh6762
@ayushkumarsingh6762 57 минут назад
Thank You Bhaiya, Brute Forse se solve krne k liye
@pseudonerd7985
@pseudonerd7985 2 часа назад
i hope the surgery to be successful stay strong !
@khursedalam1045
@khursedalam1045 2 часа назад
May Allah SWT grant your family member complete shifa and ease their pain. My thoughts and prayers are with you and your family during this difficult time.
@brandxprogaming6673
@brandxprogaming6673 2 часа назад
I was waiting for it, love you 💗
@rajeshkumar-ws5ku
@rajeshkumar-ws5ku 2 часа назад
i am waiting for you video just like someone one waiting for the movie
@peterfromengland8663
@peterfromengland8663 59 минут назад
Sir muje sapne aate h ki mik sir ese dikhte vese dikhte 🫠
@amanpaliwal3122
@amanpaliwal3122 2 часа назад
legend bhaiya
@Coder_Buzz07
@Coder_Buzz07 Час назад
70k soon❤
@raunakkumar1150
@raunakkumar1150 2 часа назад
I was waiting for this video.
@rajdeep3493
@rajdeep3493 Час назад
God bless you bhaii❤
@nehasinghggpl9637
@nehasinghggpl9637 45 минут назад
dam good bhaiya..
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 часа назад
First 🎉❤
@froglighthouse1223
@froglighthouse1223 2 часа назад
praying for your family member. stay strong
Далее
🤔Угадай постройку | WICSUR #shorts
00:59
The friendship of Ratan Tata and Shantanu Naidu
4:52
Просмотров 386 тыс.