Nikhil Lohia
Nikhil Lohia
  • Видео 252
  • Просмотров 2 943 676
Last Stone Weight (LeetCode 1046) | Full Solution with 3 different methods | Priority Queue
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin
Actual problem on LeetCode: leetcode.com/problems/last-stone-weight/
Chapters:
00:00 - Intro
00:41 - Problem Statement
03:35 - Brute Force Method
05:10 - Trying to sort
06:37 - Efficient Solution using Priority Queue
09:03 - Dry-run of Code
11:06 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Method: ruclips.net/video/kdTpUjd71G8/видео.html
Merge Sort: ruclips.net/video/-qjc4fYsZTU/видео.html
What is Big O?: ruclips.net/video/-MJ7EIV57dI/видео.html
Other Easy Problems: ruclips.net/p/PLFdAYMIVJQHMap2jOyU6-kHjQEL-vxlV2
📘 A text based explanation is available at: studyalgorithms.com
Code on ...
Просмотров: 532

Видео

Capacity to Ship Packages Within D Days (LeetCode 1011) | Full Solution with visuals and animations
Просмотров 585День назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/capacity-to-ship-packages-within-d-days/ Chapters: 00:00 - Intro 00:39 - Problem Statement 01:59 - A visual explanation 06:28 - Brute force Solution 10:30 - Efficient Solution 13:35 - Dry-run of Code 16:47 - Final Thoughts 📚 Links to topics I talk about in...
Matrix Diagonal Sum (LeetCode 1572) | Full solution with visuals | 2 possible scenarios
Просмотров 63014 дней назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/matrix-diagonal-sum/ Chapters: 00:00 - Intro 00:50 - Problem Statement 02:28 - 2 possible scenarios 05:57 - Solution 07:46 - Dry-run of Code 10:33 - Final Thoughts 📚 Links to topics I talk about in the video: Array Data Structure: ruclips.net/video/4L7exNi...
Sum of Unique Elements (LeetCode 1748) | Full solution with a frequency array
Просмотров 80121 день назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/sum-of-unique-elements/ Chapters: 00:00 - Intro 00:40 - Problem Statement 02:23 - Brute Force Method 04:01 - Frequency Array 08:21 - Dry-run of Code 10:26 - Final Thoughts 📚 Links to topics I talk about in the video: Brute Force Approach: ruclips.net/video...
Maximum Product After K Increments (LeetCode 2233) | Full solution with mathematical proof | Greedy
Просмотров 816Месяц назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/maximum-product-after-k-increments/ Chapters: 00:00 - Intro 00:39 - Problem Statement 05:12 - Mathematical Proof 09:54 - Efficient Solution 11:31 - Dry-run of Code 14:59 - Final Thoughts 📚 Links to topics I talk about in the video: Greedy Algorithmic Parad...
Minimum Size Subarray Sum (LeetCode 209) | Full solution with animations | Variable Sliding Window
Просмотров 2 тыс.Месяц назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/minimum-size-subarray-sum/ Chapters: 00:00 - Intro 00:51 - Problem Statement 03:07 - Brute Force Approach 05:05 - Sliding window with 2 pointers 12:18 - Dry-run of Code 16:16 - Final Thoughts 📚 Links to topics I talk about in the video: Brute Force Paradig...
Reverse Prefix of Word (LeetCode 2000) | Full Solution with stack data structure
Просмотров 786Месяц назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/reverse-prefix-of-word/ Chapters: 00:00 - Intro 00:43 - Problem Statement 03:12 - Using Stack Data Structure 06:24 - Dry-run of Code 08:19 - Final Thoughts 📚 Links to topics I talk about in the video: Stack Data Structure: ruclips.net/video/vrVOYj_tF7M/вид...
Reorder List (LeetCode 143) | Full Solution with 3 step process and animations
Просмотров 1,6 тыс.Месяц назад
Actual problem on LeetCode: leetcode.com/problems/reorder-list/ Chapters: 00:00 - Intro 00:40 - Problem Statement 03:51 - Brute Force Method 06:09 - Idea behind the approach 10:42 - Implementing the thought 13:08 - Dry-run of Code 15:34 - Final Thoughts 📚 Links to topics I talk about in the video: Middle of a Linked List: ruclips.net/video/IPaMfcxQtP0/видео.html Reverse a Single Linked List: ru...
Max Consecutive Ones (LeetCode 1004) | Full Solution w/ animations
Просмотров 1,7 тыс.Месяц назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/max-consecutive-ones-iii/ Chapters: 00:00 - Intro 00:52 - Problem Statement 03:05 - Brute Force Method 03:42 - Efficient Solution 10:50 - Dry-run of Code 13:33 - Final Thoughts 📚 Links to topics I talk about in the video: Max Consecutive Elements I: ruclip...
Max Consecutive Ones (LeetCode 485) | Full Solution w/ Kadane's Algorithm | Visuals and Animations
Просмотров 9332 месяца назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual Problem: leetcode.com/problems/max-consecutive-ones/ Chapters: 00:00 - Intro 00:46 - Problem Statement and Description 02:13 - Efficient Approach 07:52 - Dry-run of Code 10:20 - Applications of the problem 13:03 - Final Thoughts 📚 Links to topics I talk about in the video: Kadane's Algorithm: rucli...
Koko Eating Bananas (LeetCode 875) | Full solution with beautiful diagrams and visualizations
Просмотров 2 тыс.2 месяца назад
Join this channel to get access to perks: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin Actual problem on LeetCode: leetcode.com/problems/koko-eating-bananas/ Chapters: 00:00 - Intro 00:44 - Problem Statement 01:36 - Vizualization 08:25 - Brute Force Method 11:23 - Using Binary Search 16:53 - Dry-run of Code 19:40 - Final Thoughts 📚 Links to topics I talk about in the video: Array Data Struc...
Kth Missing Positive Number (LeetCode 1539) | Full Solution with intuition, animations and visuals
Просмотров 1,7 тыс.2 месяца назад
Kth Missing Positive Number (LeetCode 1539) | Full Solution with intuition, animations and visuals
Copy List With Random Pointer (LeetCode 138) | Full Solution with animations and 3 step process
Просмотров 1,8 тыс.2 месяца назад
Copy List With Random Pointer (LeetCode 138) | Full Solution with animations and 3 step process
Reverse Only Letters (LeetCode 917) | 2 Different Methods | Stack Data Structure and Two Pointers
Просмотров 1,1 тыс.3 месяца назад
Reverse Only Letters (LeetCode 917) | 2 Different Methods | Stack Data Structure and Two Pointers
Decode Ways (LeetCode 91) | Full Solution with visuals | Recursion to Dynamic Programming
Просмотров 3 тыс.3 месяца назад
Decode Ways (LeetCode 91) | Full Solution with visuals | Recursion to Dynamic Programming
Reverse Words In A String III (LeetCode 557) | Full solution | 3 different methods
Просмотров 4,6 тыс.3 месяца назад
Reverse Words In A String III (LeetCode 557) | Full solution | 3 different methods
Sum of Square Numbers (LeetCode 633) | Full Solution | Two pointer approach | Efficient
Просмотров 4,1 тыс.3 месяца назад
Sum of Square Numbers (LeetCode 633) | Full Solution | Two pointer approach | Efficient
Boats to Save People (LeetCode 881) | Full Solution with diagrams and visuals | Greedy
Просмотров 2,2 тыс.4 месяца назад
Boats to Save People (LeetCode 881) | Full Solution with diagrams and visuals | Greedy
Count Complete Tree Nodes (LeetCode 222) | Complete tree examples | Recursive & Iterative
Просмотров 1,9 тыс.4 месяца назад
Count Complete Tree Nodes (LeetCode 222) | Complete tree examples | Recursive & Iterative
Implement Stack using Queues (LeetCode 225) | Using single and 2 queues | Visuals and animations
Просмотров 3,7 тыс.4 месяца назад
Implement Stack using Queues (LeetCode 225) | Using single and 2 queues | Visuals and animations
Implement Queue using Stacks (LeetCode 232) | Side by side demo and diagrams
Просмотров 2,4 тыс.4 месяца назад
Implement Queue using Stacks (LeetCode 232) | Side by side demo and diagrams
Graph Data Structure | Part 12 | Graph Coloring Problem | Step by Step demo Greedy approach
Просмотров 5355 месяцев назад
Graph Data Structure | Part 12 | Graph Coloring Problem | Step by Step demo Greedy approach
Channel updates 1.0
Просмотров 8915 месяцев назад
Channel updates 1.0
Graph Data Structure | Part 11 | Dijkstra's Algorithm | Shortest Path in Graph Step by Step Demo
Просмотров 7565 месяцев назад
Graph Data Structure | Part 11 | Dijkstra's Algorithm | Shortest Path in Graph Step by Step Demo
Graph Data Structure | Part 10 | Kruskal's Algorithm | MST Step by Step Demo & Code
Просмотров 6765 месяцев назад
Graph Data Structure | Part 10 | Kruskal's Algorithm | MST Step by Step Demo & Code
Graph Data Structure | Part 9 | Prim's Algorithm | MST Step by Step Demo & Code
Просмотров 7875 месяцев назад
Graph Data Structure | Part 9 | Prim's Algorithm | MST Step by Step Demo & Code
Graph Data Structure | Part 8 | Minimum Spanning Tree | Concept & Examples
Просмотров 6575 месяцев назад
Graph Data Structure | Part 8 | Minimum Spanning Tree | Concept & Examples
Graph Data Structure | Part 7 | Weighted Directed Graph | Code & Implementation
Просмотров 9506 месяцев назад
Graph Data Structure | Part 7 | Weighted Directed Graph | Code & Implementation
Graph Data Structure | Part 6 | Breadth First Search and Traversal | BFS
Просмотров 1,2 тыс.6 месяцев назад
Graph Data Structure | Part 6 | Breadth First Search and Traversal | BFS
Graph Data Structure | Part 5 | Depth First Search and Traversal | DFS
Просмотров 1,8 тыс.6 месяцев назад
Graph Data Structure | Part 5 | Depth First Search and Traversal | DFS

Комментарии

  • @topricano9964
    @topricano9964 5 часов назад

    I'm afraid of this problem because of it's name "Kadane's Algorithm" ... After ur video I went to GeeksForGeeks and solve this also solved it in Leetcode... Thankyou so much!!... I thought DSA is very hard for me..But ur explanations made it very easyy!!

  • @hieua3137
    @hieua3137 6 часов назад

    Great Explanation !!!!

  • @rahulchandak2772
    @rahulchandak2772 15 часов назад

    The logic sounds very simple but its difficult to understand for cases where the subarray does not contain the starting (leftmost) or the ending (rightmost) element. When the sub array is in between the array

  • @rishikeshyadav4964
    @rishikeshyadav4964 17 часов назад

    Hi, great explation you keep up to the point, but I believe you can go more in depth while using the loops for, explaining how the second for loop work with some interactive example. Great work thanks

  • @user-qn9um8ud1t
    @user-qn9um8ud1t 17 часов назад

    Thank you so much for explaining each line in a very simple manner.

  • @AdityaRaj-pe3zy
    @AdityaRaj-pe3zy 18 часов назад

    most of the you tuber have taken the same example and are doing the same thing but in question we have to compare NUMS1 and NUMS2 the find it

  • @Anonymous____________A721
    @Anonymous____________A721 19 часов назад

    Literally the best one

  • @varoonsb
    @varoonsb День назад

    Thank you so much! First time seeing someone explaining solution as good as NeetCode1

  • @casstansa
    @casstansa День назад

    brilliant🤩

  • @unemployedcse3514
    @unemployedcse3514 День назад

    guys who ever is confused with '^' in Stringbuilder it is to handle below kind of tree node TreeNode root = new TreeNode(12); TreeNode subRoot = new TreeNode(2); where 12 (tweleve) will be treated as 1,2 subtree as 2 it gives true to figure out item or data in node we are using ^ as seperator still if u need confirmation pls refer test case created by nikhil in description code and nikhil small suggestion pls explain or stick to same logic during dry run , this will give more clarification and reduces doubts , in last example no null were included in ur explanation rest vedio is very helpful thank you ❤

  • @DharaniChavala
    @DharaniChavala День назад

    very nice and clear explanation.

  • @THOSHI-cn6hg
    @THOSHI-cn6hg День назад

  • @durgeshchouksey8779
    @durgeshchouksey8779 День назад

    bhai [1,1,1,1,2,2,3,3,4] and k = 2, test case hi galat hai kyuki answer unique nahi hai, it is clearly mentioned in constraints, It is guaranteed that the answer is unique. toh 2 and 3 ki freq same nahi ho sakti aur agar hogi toh k ki value 3 hogi.

  • @pt_2003
    @pt_2003 День назад

    You have a god gifted talent of explaining things in a simple manner, this is sign of a true teacher, thank you so much 🙏.

  • @Khadyot_Takale
    @Khadyot_Takale День назад

    IF C++ CODE IS FAILING this is because substring function it shoule be like s.substr(i-1,1) and s.substr(i-2,2) the second parameter is the length of the string. this should work class Solution { public: int numDecodings(string s) { int n = s.length(); vector<int> dp(n + 1); dp[0] = 1; dp[1] = s[0] == '0' ? 0: 1; for(int i = 2; i < n + 1; i++){ int d1 = stoi(s.substr(i-1,1)); int d2 = stoi(s.substr(i-2,2)); if(d1 >= 1){ dp[i] += dp[i-1]; } if(d2 >= 10 && d2 <= 26){ dp[i] += dp[i-2]; } } return dp[n]; } };

  • @Hridaymardam_
    @Hridaymardam_ День назад

    loved this really loved this!!!

  • @AzharKhan-e9m
    @AzharKhan-e9m 2 дня назад

    isme ek problem hai duplicate triplets ko lekr ... triplets double print horhe

  • @abhiroopmokshagnabheemineni
    @abhiroopmokshagnabheemineni 2 дня назад

    So, during insertion into the priority queue, it takes nlogn and also during polling it will also take nlogn. Is my understanding correct?

  • @shortandshine8279
    @shortandshine8279 2 дня назад

    Also for negative numbers how to make it work in python

  • @shortandshine8279
    @shortandshine8279 2 дня назад

    Gr8, thanks, could you please explain why cumulative and placing in output array with -1, should work

  • @yogeshganpule2695
    @yogeshganpule2695 2 дня назад

    tried watching but you just explained what the code does and its too confusing for me to undertsand what for each freq [c -'a']++; Does

    • @nikoo28
      @nikoo28 2 дня назад

      c is the actual character…let us say it is character ‘k’ And we do c - ‘a’ What happens internally is both are converted to ascii Small ‘a’ has ascii 97 The character ‘k’ has ascii 107 So when you do c - ‘a’ we get 107-97 = 10 That means freq[10] This represents 11th character as array is 0 based indexing Means we are referring to frequency of 11th character in the English alphabet which is letter ‘k’

    • @yogeshganpule2695
      @yogeshganpule2695 День назад

      @@nikoo28 Thanks .

  • @SubhajitDas-mt7sn
    @SubhajitDas-mt7sn 2 дня назад

    for cpp : class Solution { public: int sumOfUnique(vector<int>& nums) { unordered_map<int,int>mp; for(int i:nums) mp[i]++; int sum=0; for(auto it:mp){ if(it.second==1) sum+=it.first; } return sum; } };

  • @kavinkavin3525
    @kavinkavin3525 2 дня назад

    Impressive

  • @emma_anime
    @emma_anime 2 дня назад

    thank you bro

  • @mdshahidansari9126
    @mdshahidansari9126 2 дня назад

    Awesome explanation sir

  • @AzharKhan-e9m
    @AzharKhan-e9m 3 дня назад

    new subscriber

  • @rishabhshukla8180
    @rishabhshukla8180 3 дня назад

    Nikhil, your code would not work for the test case you mentioned: [1,1,1,1,2,2,3,3,4] & k=2 This code is getting submitted on Leetcode because there it is mentioned that unique answers only. But in the about test case: We should get [1,2,3] as ans for k=2. You cannot assume the res array of size k since there might be duplicacy. Otherwise solution works fine for the Leetcode problem. Here is the Code which will cover duplicacy as well. class Solution { public static int[] topKFrequent(int[] nums, int k) { int n = nums.length; List<Integer>[] bucket = new ArrayList[n + 1]; HashMap<Integer, Integer> frequencyMap = new HashMap<>(); ArrayList<Integer> resultList = new ArrayList<>(); for (int num : nums) { frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); } for (int i = 0; i <= n; i++) { bucket[i] = new ArrayList<>(); } frequencyMap.forEach( (element, frequency) -> { bucket[frequency].add(element); }); for (int i = n; i >= 0; i--) { if (bucket[i] != null) { resultList.addAll(bucket[i]); if (resultList.size() >= k) { break; } } } int[] result = new int[resultList.size()]; for (int i = 0; i < result.length; i++) { result[i] = resultList.get(i); } return result; }

  • @tahsanrafi2884
    @tahsanrafi2884 3 дня назад

    explanation was amazing

  • @sksabir8063
    @sksabir8063 3 дня назад

    🤩

  • @topricano9964
    @topricano9964 3 дня назад

    ur explanation is amazing..Thankyou sm!!!

  • @nishanthsunkara1160
    @nishanthsunkara1160 3 дня назад

    Before watching this video, watch Counting Sort, its a prerequisite.

  • @arijitmalakar6911
    @arijitmalakar6911 3 дня назад

    This solution is giving "time limit exceeded" error n leetcode...

  • @arunvasu30
    @arunvasu30 3 дня назад

    Bhai start dsa course atleast for members, ill buy a membership for sure.

  • @abhishekj2096
    @abhishekj2096 3 дня назад

    This solution has runtime of 10ms and beats only 68%. Do you have a faster solution?

    • @nikoo28
      @nikoo28 3 дня назад

      don't go by these numbers you see on leetcode. If your solution is accepted, it is usually a good enough solution. The runtime usually depends on a lot of things: - the startup time of the VM - the language choice - the processor of the VM - the version of software stack

  • @piyushranjan5910
    @piyushranjan5910 3 дня назад

    Sir I've a better approach for this We can set the minimum value to the (maximum value/no of days) Because the range can better shorten by this logic.

    • @nikoo28
      @nikoo28 3 дня назад

      that is also a way to make things quicker. but the worst case time complexity will still remain the same :)

  • @riyarb559
    @riyarb559 3 дня назад

    awesome solution buddy!!

  • @ridj41
    @ridj41 3 дня назад

    Definitely leetcode tagged this wring it is a medium level problem

  • @vinaytiwari6651
    @vinaytiwari6651 4 дня назад

    Sir u ar doing work plz keep doing it... I will promote your channel... U got great content truly helpful ❤❤❤❤

    • @nikoo28
      @nikoo28 3 дня назад

      So nice of you...please help as much as you can

    • @AmandeepSingh-uz1ug
      @AmandeepSingh-uz1ug День назад

      @@nikoo28 Keep going sir

  • @rickdutta3935
    @rickdutta3935 4 дня назад

    This is just a fibonacci series answer.

    • @nikoo28
      @nikoo28 3 дня назад

      but you need to reach over there first :)

    • @rickdutta3935
      @rickdutta3935 3 дня назад

      @@nikoo28 Love your videos sir❤

  • @srivyshnavi6271
    @srivyshnavi6271 4 дня назад

    how to get that memozation logic?? like how will I know the rules of filling the table and analysing the answer??

    • @nikoo28
      @nikoo28 3 дня назад

      how do you understand any logic?? just by sheer practice...once you solve more problems, you will start seeing patterns.

  • @ITRSudharsanR
    @ITRSudharsanR 4 дня назад

    Good work sir 👏

  • @InfiniteRedditEpisodes
    @InfiniteRedditEpisodes 4 дня назад

    I'm confused, Why do we mark a node as visited as soon as we add it into the queue as oppose to while we are actually polling it in the code? in DFS we only marked a node as visited when we popped it from the stack.

    • @nikoo28
      @nikoo28 3 дня назад

      you can mark it visited anytime. Watch my video on DFS, i mark it visited as soon as I add it

  • @urvashichaurasia6284
    @urvashichaurasia6284 4 дня назад

    Nice explanation sir

  • @nikoo28
    @nikoo28 4 дня назад

    As of this writing LeetCode added a new test case which cause the above code to fail. The logic is still correct though. The test case fails because of overflow error. With just a small fix, the code still works. Have a look at the updated code on my Github link :)

  • @heyneisha3816
    @heyneisha3816 4 дня назад

    Soooo I have watched this video multiple times and have looked at the question you show in the beginning of the video. Though I understand what you’re doing with using an index to identify duplicates what I don’t understand how this would work based off your question without knowing the range of numbers in the array. Your example uses something like this: [2,4,1,6,3,1]. How will this solution work with an array like this based off the question?[3,16,1,4,2,1].

    • @nikoo28
      @nikoo28 3 дня назад

      read the problem statement again. "Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive." So for your test case when there is a 16, there should be atleast 16 elements in the array. Always read the problem constraints...they do give out a very good hint :)

    • @heyneisha3816
      @heyneisha3816 3 дня назад

      @@nikoo28 where is this question in your video? Please reread the question you showed us in the video.

  • @adarshsanthoshkumar3617
    @adarshsanthoshkumar3617 4 дня назад

    This is work ❤❤

  • @user-hp9kj8qt1h
    @user-hp9kj8qt1h 4 дня назад

    Girl did 2 sum and 3 sum today!! Keep going, faang is waiting for me

    • @nikoo28
      @nikoo28 3 дня назад

      You can do it!

  • @user-sc1ld7ss3k
    @user-sc1ld7ss3k 4 дня назад

    amazing

  • @prapti2385
    @prapti2385 5 дней назад

    The solution is wrong. Also why did you convert all the remaining elements to zero in the last step? If you take the example from striver's video he particularly said about that element.

    • @nikoo28
      @nikoo28 4 дня назад

      what do you mean when you say the solution is wrong? The solution passes all the given test cases on LeetCode.

  • @rudrajitpaul4088
    @rudrajitpaul4088 5 дней назад

    i did not understand fast=nums[nums[fast]] .if nums[fast] is 3?

    • @nikoo28
      @nikoo28 4 дня назад

      then fast = nums[3]