Xavier Elon
Xavier Elon
  • 99
  • 371 927
HackerRank | Minimum Swaps 2 | Solution Explained (Java + Whiteboard)
Today I go over a medium HackerRank problem based on arrays. Please comment below if you have any questions!
Running Time: O(N)
Space Complexity: O(1)
Social media:
Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ
Github: github.com/xavierelon1
Facebook: xavier.hollingsworth.3
Instagram: xavierelon
LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/
Twitter: elon_xavier
Переглядів: 5 798

Відео

HackerRank | Binary Trees | How to Find Height of Binary Tree | Solution Explained (Java)
Переглядів 2,7 тис.3 роки тому
Running Time: O(N) Space Complexity: O(N) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 143 | Reorder List | Solution Explained (Whiteboard + Algorithm in Java)
Переглядів 2,4 тис.3 роки тому
Running Time: Space Complexity: Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 567 | Permutation in String | Solution Explained (Whiteboard + Algorithm in Java)
Переглядів 15 тис.3 роки тому
Running Time: O(t) Space Complexity: O(1) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 785 | Is Graph Bipartite? | Solution Explained (Whiteboard + Algorithm in Java)
Переглядів 2,6 тис.3 роки тому
Running Time: O(V E) Space Complexity: O(V) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 116 | Path Sum II | Solution Explained (White + Algorithm in Java)
Переглядів 1,1 тис.3 роки тому
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, Running Time: O(N^2) Space Complexity: O(N) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook...
LeetCode 116 | Populating Next Right Pointers in Each Node
Переглядів 7 тис.3 роки тому
Running Time: O(N) Space Complexity: O(1) You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Init...
LeetCode 953 | Verifying an Alien Dictionary | Solution Explained (Java + Whiteboard)
Переглядів 9693 роки тому
Running Time: O(N) Space Complexity: O(N) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 112 | Path Sum | Solution Explained | Java + Whiteboard
Переглядів 1,4 тис.3 роки тому
Running Time: O(N) Space Complexity: O(N) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 129 | Sum Root to Leaf Numbers | Solution Explained (Java + Whiteboard)
Переглядів 1,1 тис.3 роки тому
Running Time: O(N) Space Complexity: O(H) Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1-2-3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: git...
LeetCode 123 | Best Time to Buy and Sell Stock III | Solution Explained (Java + Whiteboard)
Переглядів 2,4 тис.3 роки тому
Running Time: O(N) Space Complexity: O(1) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 122 | Best Time to Buy and Sell Stock II | Solution Explained (Java + Whiteboard)
Переглядів 8303 роки тому
Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [...
LeetCode 121 | Best Time to Buy and Sell Stock | Solution Explained in Java
Переглядів 5813 роки тому
Running Time: O(N) Space Complexity: O(1) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: xavierelon LinkedIn: www.linkedin.com/in/xavier-hollingsworth-524144127/ Twitter: elon_xavier
LeetCode 349 | Intersection of Two Arrays | Solution Explained (Java)
Переглядів 6 тис.3 роки тому
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2] Running Time: O(N M) Space Complexity: O(N M) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: x...
LeetCode 366 | Find Leaves of Binary Tree | Solution Explained (Java + Whiteboard)
Переглядів 9 тис.4 роки тому
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Running Time: O(N) Space Complexity: O(N) Always be pluggin: Slack Channel: join.slack.com/t/xavierelonleetcode/shared_invite/zt-fdzjx82o-uQgTCJtFy70OENdqOitttQ Github: github.com/xavierelon1 Facebook: xavier.hollingsworth.3 Instagram: ...
LeetCode 315 | Sum of Nodes with Even-Valued Grandparent | Solution Explained (Java + Whiteboard)
Переглядів 1,3 тис.4 роки тому
LeetCode 315 | Sum of Nodes with Even-Valued Grandparent | Solution Explained (Java Whiteboard)
LeetCode 713 | Subarray Product Less Than K | Solution Explained (Java + Whiteboard)
Переглядів 2,8 тис.4 роки тому
LeetCode 713 | Subarray Product Less Than K | Solution Explained (Java Whiteboard)
LeetCode 1 | Two Sum | All Solutions Explained | Java + Whiteboard
Переглядів 2,7 тис.4 роки тому
LeetCode 1 | Two Sum | All Solutions Explained | Java Whiteboard
LeetCode 590 | N-ary Tree Postorder Traversal
Переглядів 1,6 тис.4 роки тому
LeetCode 590 | N-ary Tree Postorder Traversal
LeetCode 589 | N-Ary Tree Pre-Order Traversal | Iterative + Recursive Solution (Java + Whiteboard)
Переглядів 1,8 тис.4 роки тому
LeetCode 589 | N-Ary Tree Pre-Order Traversal | Iterative Recursive Solution (Java Whiteboard)
LeetCode 965 | Univalued Binary Tree | Solution Explained (Java)
Переглядів 7084 роки тому
LeetCode 965 | Univalued Binary Tree | Solution Explained (Java)
LeetCode 350 | Intersection of Two Arrays II | Solution Explained (Java + Whiteboard)
Переглядів 18 тис.4 роки тому
LeetCode 350 | Intersection of Two Arrays II | Solution Explained (Java Whiteboard)
How I Passed My AWS Cloud Practitioner Exam in 10 Days (as a non-millionaire)
Переглядів 2,3 тис.4 роки тому
How I Passed My AWS Cloud Practitioner Exam in 10 Days (as a non-millionaire)
LeetCode 152 | Maximum Product Subarray | Solution Explained (Java + Whiteboard)
Переглядів 16 тис.4 роки тому
LeetCode 152 | Maximum Product Subarray | Solution Explained (Java Whiteboard)
HackerRank | Repeated String | Solution Explained (Java)
Переглядів 4,6 тис.4 роки тому
HackerRank | Repeated String | Solution Explained (Java)
HackerRank | Equalize the Array | Solution Explained (Java + Whiteboard)
Переглядів 2,5 тис.4 роки тому
HackerRank | Equalize the Array | Solution Explained (Java Whiteboard)
LeetCode 264 | Ugly Number II | Solution Explained (Whiteboard + Java)
Переглядів 5 тис.4 роки тому
LeetCode 264 | Ugly Number II | Solution Explained (Whiteboard Java)
LeetCode 1512 | Number of Good Pairs | Solution Explained (Java + Whiteboard)
Переглядів 5 тис.4 роки тому
LeetCode 1512 | Number of Good Pairs | Solution Explained (Java Whiteboard)
LeetCode 263 | Ugly Number | Solution Explained (Java)
Переглядів 4,6 тис.4 роки тому
LeetCode 263 | Ugly Number | Solution Explained (Java)
LeetCode 680 | Valid Palindrome II | Greedy Algorithm Explained (Java)
Переглядів 7874 роки тому
LeetCode 680 | Valid Palindrome II | Greedy Algorithm Explained (Java)

КОМЕНТАРІ

  • @shipinytb
    @shipinytb Місяць тому

    Awesome solution! Thanks for sharing~ Very curious how did you come up with this solution and why we can get the *minimum* swaps in this way.🤔

  • @kevinrodriguez7403
    @kevinrodriguez7403 Місяць тому

    when using this solution my output is just [2]. Here is the solution I submitted on leetcode: class Solution { public int[] intersect(int[] nums1, int[] nums2) { HashSet<Integer> set1 = new HashSet<>(); HashSet<Integer> intersect = new HashSet<>(); for(Integer i : nums1){ set1.add(i); } for(Integer i : nums2){ if(set1.contains(i)){ intersect.add(i); } } int size = intersect.size(); int[] ans = new int[size]; int index = 0; for(Integer i : intersect){ ans[index++] = i; } return ans; } }

  • @RuichenZhao
    @RuichenZhao Місяць тому

    Four lines of code makes this hard question looks surprisingly easy

    • @xavierelon
      @xavierelon Місяць тому

      The hard part is coming up with those 4 lines on the spot

  • @abhigoku2022
    @abhigoku2022 Місяць тому

    The debugger really helped.... Thanks for showing the code through the debugger

  • @beens3865
    @beens3865 2 місяці тому

    I was hooked as soon as I heard shook ones pt. 1 by mob deep. I am tired of watching neetcode.

    • @xavierelon
      @xavierelon 2 місяці тому

      @@beens3865 haha thank you. It’s a classic

  • @Krishna-y5j6o
    @Krishna-y5j6o 2 місяці тому

    Very nice technique to explain sir

  • @abdihaashin1350
    @abdihaashin1350 2 місяці тому

    man you are good. i like the way you explain thanks

  • @BtXFWkyZBtXFWkyZ
    @BtXFWkyZBtXFWkyZ 3 місяці тому

    The solution is incorrect

  • @BtXFWkyZBtXFWkyZ
    @BtXFWkyZBtXFWkyZ 3 місяці тому

    It doesn't work with input [0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0]

  • @coleeastman4910
    @coleeastman4910 3 місяці тому

    Decent explanation, but I am not passing one test case on LeetCode

  • @samfromuk796
    @samfromuk796 3 місяці тому

    Well explained

  • @abhishekkarn8918
    @abhishekkarn8918 3 місяці тому

    You missed explaining the part where the actual explanation was needed.

  • @scholarway
    @scholarway 4 місяці тому

    We don't count the answer as part of the space complexity. We use The extra space here for ans and the recursion call stack. The depth of the call stack is equal to the length of ans, which is limited to k. Therefore: Space complexity: O(k)

  • @skyjoe3655
    @skyjoe3655 4 місяці тому

    Gee what kind of person can come up with this idea? I was able to make O(Qn) algorithm with time limit exceeds for some test cases.

  • @dilshodinomov5993
    @dilshodinomov5993 5 місяців тому

    why (0,1) map.put(0, 1)

  • @ladro_magro5737
    @ladro_magro5737 5 місяців тому

    I don't think this is a nice explanation. Realistically, you didn't even know how to solve a problem without looking at the cheat sheet, which means you don't actually fully understand the solution. And since you do not understand the solution it is impossible to explain it properly. My recommendation is to study the solution deeply before creating any future videos.

  • @nishukumari8791
    @nishukumari8791 5 місяців тому

    waw its really simple and awesome

  • @ladro_magro5737
    @ladro_magro5737 5 місяців тому

    So, does anyone understands this problem at all, or are we all just copy pasting the same exact solution without actually understanding what's going on? I can tell by the way Xavier explained it, that he doesn't get it at all. He just memorized the code. And the explanation was done on a [1,1,1] array. Imagine if it was [7,3,2,1,8,5,4,9], int k = 6. He would go into an infinite loop of confusion.

    • @guiquansun4720
      @guiquansun4720 27 днів тому

      Could you please explain what the infinite loop means here?

  • @ThainguyenVietnamBeauties
    @ThainguyenVietnamBeauties 6 місяців тому

    I've gotten stuck on this problem for a few days. Your recursion is neat and clearly shows the algorithm. Thanks guy!

  • @Fanaro
    @Fanaro 7 місяців тому

    You just gave out the answer on the whiteboard. That's not an explanation.

    • @xavierelon
      @xavierelon 7 місяців тому

      Okay thanks for the feedback. I'll try better next time

  • @Lotuz
    @Lotuz 7 місяців тому

    Coming here after a long time just to thank you Xavier, for the comment in the beginning of the video. About not knowing how could someone come up with this on their own. I've struggled a lot with the idea that some people are just so incredibly much smarter and better at this than I am. And thats ok that they are. But at the same time some people are just more educated, have more study, have more contact with these challenges and patterns, as I have more contact with other stuff, and remembering that is very good.

  • @ayushman_sr
    @ayushman_sr 7 місяців тому

    video editing is epic

  • @outofboundsbro
    @outofboundsbro 8 місяців тому

    I dislike this question immensely. The problem description leads you to think you gotta replace all those individual indexes. I personally feel like the problem intentionally misleads you.

  • @philipthomas6155
    @philipthomas6155 8 місяців тому

    Thank you no one else explained how the removal part working.Great video!! Keep Uploading

  • @Dsalas96
    @Dsalas96 9 місяців тому

    Amazing explanation! Thank you so much for the help on this <3

  • @artemvolkov5682
    @artemvolkov5682 9 місяців тому

    So smart to add background music while you're unable to articulate well the n/m complexity. I've broken my ears

  • @martinberger365
    @martinberger365 10 місяців тому

    Bit confusing 😂

  • @tamalbarman
    @tamalbarman 10 місяців тому

    static long repeatedString(String s, long n) { int strLength = s.length(); long q = 0, r = 0; q = n / strLength; r = n % strLength; long partialStrLen = (r == 0) ? 0 : r; long aCount = q * getLetterCount(s, s.length()) + getLetterCount(s, partialStrLen); return aCount; } public static long getLetterCount(String s, long strLength) { long count = 0; for (int i = 0; i < strLength; i++) { if (s.charAt(i) == 'a') count++; } return count; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); long n = sc.nextLong(); long aCount = repeatedString(s, n); System.out.println(aCount); sc.close(); }

    • @DeRan9ed87
      @DeRan9ed87 4 місяці тому

      static long repeatedString(String s, long n) { int strLength = s.length(); long q = 0, r = 0; q = n / strLength; // that ternary excess I think because in case r == 0 ternary return 0 same as main flow =) long partialStrLen = (r == 0) ? 0 : r; long partialStrLen = n % strLength; long aCount = q * getLetterCount(s, s.length()) + getLetterCount(s, partialStrLen); return aCount; } but my option is: public static long repeatedString(String s, long n) { // terrible cause of casting (int) n % s.length() return -17 instead of 70 // for the case "epsxyyflvrrrxzvnoenvpegvuonodjoxfwdmcvwctmekpsnamchznsoxaklzjgrqruyzavshfbmuhdwwmpbkwcuomqhiyvuztwvq", // 549382313570L, // 16481469408L var a = Integer.valueOf(String.valueOf(n % s.length())); return s.length() > n ? getOccurences(s.substring(0, (int) Math.abs(n - s.length()) + 1)) : getOccurences(s) * (n / s.length()) + getOccurences(a > 0 ? s.substring(0, a) : ""); } private static long getOccurences(String value) { return value.codePoints().filter(i -> i == 'a').count(); }

  • @wenkaiyang1487
    @wenkaiyang1487 11 місяців тому

    what is helper function used for?

  • @athangkulkarni8245
    @athangkulkarni8245 11 місяців тому

    At max, minimum of size of both arrays can be duplicates, so instead of doing length1 * length2, you can do min(length1, length2)

  • @ayushmandhar9215
    @ayushmandhar9215 11 місяців тому

    solved using similar concept , but use recussion , although liked your solution more

  • @AshrafMMA
    @AshrafMMA Рік тому

    I'm trying to do a level order traversal stores the node in list<list<node>> and then for each level, in an interation, i updates their right to next node's address. -- does not work tho.

  • @gulfstream1800
    @gulfstream1800 Рік тому

    it's enough to find one unbalanced subTree to say that the whole tree is unbalanced class Solution { boolean isBalanced = true; public boolean isBalanced(TreeNode root) { if (root == null) return true; dfs(root); return isBalanced; } private int dfs(TreeNode root) { if (root == null) return -1; int left = dfs(root.left); int right = dfs(root.right); if (Math.abs(left - right) > 1) { isBalanced = false; } return Integer.max(left + 1, right + 1); } }

    • @artvonsatwo3044
      @artvonsatwo3044 7 місяців тому

      Thanks , the complexity in here is way lower.

  • @dinohunter7176
    @dinohunter7176 Рік тому

    Nice solution, made one with a lot of apparently useless if else statements.

  • @dailychillvibe6012
    @dailychillvibe6012 Рік тому

    bruh with one sentence, you answer the question that I've been struggling for days. I really don't understand the question XD

  • @m3hdim3hdi
    @m3hdim3hdi Рік тому

    the game[i] = 1 part is very smart

  • @coushikgobi6473
    @coushikgobi6473 Рік тому

    your code had some errors: this soltuion passed the testcases kindly verify: class Solution { public Node connect(Node root) { if (root == null) { return null; } Node levelStart = root; while (levelStart != null) { Node current = levelStart; Node dummy = new Node(); // Dummy node to maintain the structure Node tail = dummy; while (current != null) { if (current.left != null) { tail.next = current.left; tail = tail.next; } if (current.right != null) { tail.next = current.right; tail = tail.next; } current = current.next; } levelStart = dummy.next; // Move to the next level } return root; } }

  • @raj_kundalia
    @raj_kundalia Рік тому

    thank you!

  • @TonyFangtf245yay
    @TonyFangtf245yay Рік тому

    So you're basically looping from the back of the array and testing each element as if it was bribed. Would it be possible to loop from the front of the array and test each element as if it was the briber?

  • @sadBoiDev
    @sadBoiDev Рік тому

    "I don't know how you come up with this unless your really smart" Ya thats the vibe every time I cant do a problem in O(n) time =[

  • @luvingskittles
    @luvingskittles Рік тому

    From a more mathematical perspective: Let x = distance from starting node to node where cycle begins Let y = distance from node where cycle begins to where fast and slow pointer meets Let c = length of the cycle Let n = number of cycles travelled When slow and fast meet: slow pointer travelled x + y distance fast pointer travelled 2(x + y) distance -> extra distance travelled is a multiple of cycle length -> 2(x + y) - (x + y) = nc -> x + y = nc (also means slow travelled distance of nc) -> x = nc - y We can infer than if slow pointer continues travelling from (x + y), after x more steps it will reach the cycle starting point. Which is why if we move head at the same rate as slow in the second part, their meeting point equals to start of cycle.

  • @jianlu9841
    @jianlu9841 Рік тому

    I had this problem interviewed at Google back in 2021, but not binary tree, it was N-ary Tree. I was using topological sorting to resolve it.(make every node pointing to its parent node) Not at good as yours though.

  • @JordanTaylor-y4f
    @JordanTaylor-y4f Рік тому

    Thank you. I have always wondered why there was a pre-increment and a post increment, and this is a perfect use case for post-increment. It's the small things sometimes. I've never seen that before.

  • @onurozen1
    @onurozen1 Рік тому

    good solution, thank you

  • @TonyFangtf245yay
    @TonyFangtf245yay Рік тому

    Where can we learn more about dynamic programming for problems related?

  • @psg9278
    @psg9278 Рік тому

    4:41

  • @mojojojo1211
    @mojojojo1211 Рік тому

    Thanks I was breaking my head over this

  • @alfredawn7939
    @alfredawn7939 Рік тому

    Thanks for mang this helpful vid, just wondering. What does the int columns A[0].length do?>

  • @CSChitradeviS
    @CSChitradeviS Рік тому

    This one🔥

  • @AnaGuerra03
    @AnaGuerra03 Рік тому

    hi i am sorry if i am bothering you... in this problem i tried to make like a duplicate and make one go to the right and the other to the left and little by little replace them one of them would be you used as a sort of pointer to guide which value goes next and the other one to change the values. something like this: class Solution { public TreeNode invertTree(TreeNode root) { return mirrorTree(root,root); } public TreeNode mirrorTree(TreeNode normalTree,TreeNode invertedTree) { if(normalTree != null) { mirrorTree (normalTree.right, invertedTree.left); invertedTree.val = normalTree.val; mirrorTree (normalTree.left, invertedTree.right); }return invertedTree; } } but it did not work it only works with half, i assume it gets normalTree and invertedTree as same tree when it should be different but i am not sure. Just started programming about a month ago and i am still new at this.