LeetCode Fruit Into Baskets Solution Explained - Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ต.ค. 2024

ความคิดเห็น • 60

  • @KaranSharma-ew7io
    @KaranSharma-ew7io 4 ปีที่แล้ว +20

    Nick refers to kevin in this video , finally secret unveiled 😆😆

  • @dayemonblackfyre9886
    @dayemonblackfyre9886 5 ปีที่แล้ว +38

    A hashmap that stays size 2 is O(1) space.

  • @dipayandev7441
    @dipayandev7441 3 ปีที่แล้ว +9

    Only problem with this solution is when you have K baskets. Will you be keeping K variables then?

  • @B-Billy
    @B-Billy 3 ปีที่แล้ว +3

    We can also put the code of last if block into the else part where you checked fruit==last_fruit.

  • @shellysmith4369
    @shellysmith4369 3 ปีที่แล้ว +5

    Code review from a future Tech Lead at Google: :D
    Prefer for loop to for each loop. Boxing and unboxing Integer/int values is expensive and unnecessary. Switching to a for loop will half your runtime.
    This isn't C, in Java land we don't use underscores, camelCase is preferred.
    '++' is more conventional than '+= 1'
    3rd "if" is unnecessary it's the same condition as the previous else condition.
    HashMap of constant size is not more space in big O notation (still O(1) space). It's constant space as it does not grow with the size of the input array.
    You actually generate far more memory allocations boxing and unboxing memory in your for each loop.
    Overall you are a great problem solver but a little sloppy. I look forward to watching some of your more recent videos to see how you have progressed. :)

    • @jomalomal
      @jomalomal 2 ปีที่แล้ว +1

      this is a very thoughtful code review; bet you're doing great at your new position :D

    • @NaveenKumar-os8dv
      @NaveenKumar-os8dv 2 ปีที่แล้ว

      future tech lead, please hire me. lol

  • @palak2708
    @palak2708 4 ปีที่แล้ว +3

    Great video, I spent so much time on leetcode solving this, then as soon as I saw "can be solved using sliding window". Solved it in 5 minutes. Need to memorize which question uses what!

    • @jamjam3448
      @jamjam3448 2 ปีที่แล้ว

      so far as the question involves contiguous subarray then sliding window can be used

  • @_wise_one
    @_wise_one 4 ปีที่แล้ว +4

    I have this weird problem, when I go to leetcode to try some problems, I can't figure out how to solve (50% of the time), but when I watch your video just for first 1-2 min (until you explain the problem), and then I try to solve, I get all the solutions in my mind and solve that exact problem without watching the rest of your video and after solving the problem when I compare your sol to mine, its same(~80%) :)

  • @mrmca1
    @mrmca1 4 ปีที่แล้ว +4

    That other dude is @Kevin Naughton Jr.

  • @stith_pragya
    @stith_pragya ปีที่แล้ว +1

    Thank You For the video Nick ..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @michaeldang8189
    @michaeldang8189 4 ปีที่แล้ว +2

    “What is the most amount of fruit you can carry", where is the word "most" in the question?

  • @Tlacoyo59a
    @Tlacoyo59a 4 ปีที่แล้ว

    I saw other videos but the best explanation for this problem is yours. The sliding window.

  • @chrisalbers1370
    @chrisalbers1370 ปีที่แล้ว

    instead of for each I did:
    for (int i = 0, fruitsLength = fruits.length; i < fruitsLength; i++) {
    int fruit = fruits[i];

  • @qwarlockz8017
    @qwarlockz8017 3 ปีที่แล้ว +1

    You are the man... I still suck at these... but watching you solve and transcribing... makes me understand them a little bit more.. so thanks for being a rock star!

  • @alisleiman724
    @alisleiman724 4 ปีที่แล้ว +2

    hey nick ,go check that guy Mr K solution of merging KSorted list .he solved with very bade time and space complexity .rather then just adding the head only for the heap,he added all the element of all the lists .yes you are smarter ,but u need to work on your explanation .not all people like me and u can easily understand complicated stuff.good luck man .

  • @mrfred456123
    @mrfred456123 4 ปีที่แล้ว +2

    Nick White is pumped up..!!!

  • @nikhilsrivastava7719
    @nikhilsrivastava7719 ปีที่แล้ว

    This approach gets easier to understand after understanding the hash map approach. So try both.

  • @pushkarraj3839
    @pushkarraj3839 4 ปีที่แล้ว +3

    haha nick your solution is fab. And i know hashmap dude...you nailed it!!! if you could help me with
    1) Expressive Words,
    2)Most Stones Removed with Same Row or Column
    ,3) Evaluate Division
    and 4th Maximize Distance to Closest Person
    all of them on leetcode and i am struggling with these

  • @chengkunliu6948
    @chengkunliu6948 2 ปีที่แล้ว

    Thank you, Sir. Brilliant!

  • @pulkitindora
    @pulkitindora 3 ปีที่แล้ว

    Really liked this video, one of your best work dude.

  • @sreedeviatgmail
    @sreedeviatgmail 2 ปีที่แล้ว

    I really wish the variable names were more suited to the problem.

  • @giorgi23
    @giorgi23 4 ปีที่แล้ว +1

    Amazing. Thanks for this

  • @geekyNib
    @geekyNib ปีที่แล้ว

    Hey @Nick ...u very much resembles like a character in Harry Potter..

  • @piyushsharma1638
    @piyushsharma1638 5 ปีที่แล้ว +1

    Thanks for easy explanation ;)

  • @hhcdghjjgsdrt235
    @hhcdghjjgsdrt235 2 ปีที่แล้ว

    Sliding windows are the most irritating one

  • @mthatipamula
    @mthatipamula 4 ปีที่แล้ว

    Thank you Nick. I do know the guy you are speaking about. I agree your solution is good. Please leave your twitter handle if you are on it.

  • @daipayanhati2347
    @daipayanhati2347 4 ปีที่แล้ว +2

    did you mean kevin ;-)

  • @raahuldutta
    @raahuldutta 4 ปีที่แล้ว

    Awesome Dude..Thanks for the explanation :)

  • @jugsma6676
    @jugsma6676 4 ปีที่แล้ว

    If following along with too many if-else statements in the video, here's the easily readable and understandable code
    def max_fruits_in_basket(trees):
    basket1= -1; c_b1 = 0
    basket2 = -1; c_b2 = 0
    max_counts = 0
    for fruit in trees:
    if fruit == basket2:
    c_b2 += 1
    if fruit == basket1:
    c_b1 += 1
    if (fruit != basket1) and (fruit != basket2):
    max_counts = max(max_counts, (c_b1 + c_b2))
    basket2 = basket1
    c_b2 = c_b1
    basket1 = fruit
    c_b1 = 1
    return max_counts

    • @gurudutt83
      @gurudutt83 3 ปีที่แล้ว

      Won't work for {1,2,1,1,4,4,5,5,4,5}

  • @HarishAmarnath
    @HarishAmarnath 3 ปีที่แล้ว +1

    This video did not age well. The hash map is be the better approach, in case the no of basket increase your code won't work

  • @saurabhjaiswal2079
    @saurabhjaiswal2079 4 ปีที่แล้ว

    Dude, amazing. I spent a lot of time-solving this question and finally, after watching your video I understood the problem. Still, it was a little difficult to understand your solution for a beginner like me. It will be really good if you can explain this in more detail.

    • @bhaskarpandey8586
      @bhaskarpandey8586 4 ปีที่แล้ว +1

      Bro he has explained in great detail already !, if you are stuck then I recommend watching some easy related problems to get the hang of it. A good start would be Kadane's algorithm.

  • @tommyypoon
    @tommyypoon 4 ปีที่แล้ว

    this is such a poorly worded problem
    Input:
    [1,0,1,4,1,4,1,2,3]
    Output:
    6
    Expected:
    5
    I kept thinking that i had to maintain the count of the secondLastFruit, so the answer would be [1,1,4,1,4,1]
    but this problem is actually find the largest subarray that contains 2 numbers which is [1,4,1,4,1]

    • @fabtjar
      @fabtjar 4 ปีที่แล้ว +1

      It's says add fruit to your basket. If you cannot then stop. You cannot add more that 2 types so it stops at the 4.

  • @saravanadharmaraj1527
    @saravanadharmaraj1527 4 ปีที่แล้ว

    Good one!

  • @oscaropdyou
    @oscaropdyou 4 ปีที่แล้ว

    I would probably use slightly more simple approach. More readable:
    for (int i = 2; i < trees.length; i++) {
    int fruit = trees[i];
    if (fruit != lastFruit && fruit != secondLastFruit) {
    isNewFruit = true;
    } else {
    isNewFruit = false;
    }
    if (!isNewFruit) {
    currentCount++;
    if (fruit == lastFruit) {
    lastFruitCount++;
    } else {
    lastFruitCount = 1;
    }
    } else {
    secondLastFruit = lastFruit;
    lastFruit = fruit;
    lastFruitCount = 1;
    currentCount = 2;
    }
    maxCount = Math.max(maxCount, currentCount);
    }

  • @kratigupta80
    @kratigupta80 4 ปีที่แล้ว

    Savior

  • @RanjuRao
    @RanjuRao 3 ปีที่แล้ว

    Both the videos(yours and hashmap guy) didn't go well in explaining "how you arrived to the solution",Everybody knows its a sliding window approach , what we need is to have simple and non-clumsy explanation. Your approach works for basket=2 and you did not optimise anything (using four counter variables) and your video 's goal should be to make people understand and benefit from your coaching, neither did hashmap video guy explained it better. What the audience is looking for making the underlying concept clear ! P.S " no hard feelings/hatred comments" ,Just a disappointed user.

    • @NickWhite
      @NickWhite  3 ปีที่แล้ว +1

      just letting you know but i made all these leetcode videos with no subscribers at the time so i didn’t expect to be teaching at such large scale otherwise i would’ve tried a lot harder sorry!

    • @RanjuRao
      @RanjuRao 3 ปีที่แล้ว +1

      @@NickWhite I appreciate your contribution to other problems where I have liked and saved it for future reference, but honestly I strongly feel the purpose of video was to help people , we can still get ready code in "leetcode " discussions forum/google itself, we come here to get in depth solution /why it works the way it is. There is no doubt you're good programmer ,but some times we need little more than that, a better coach. I have not come here to put hatred comments ,just providing my inputs when I had a hope that this video would actually make me understand to the problem's underlying concept(after going over 10 videos for this problem solution). Again , I appreciate for the efforts in all your videos .Keep doing the good work.

    • @NickWhite
      @NickWhite  3 ปีที่แล้ว +1

      @@RanjuRao yeah no im just trying to explain to you i made all of these not really with the idea that a lot of people would watch them i was more so just documenting myself doing them and some people watched them

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 ปีที่แล้ว

    Nice explanation , I have watched same video from another TH-camr but I didn't understand. Thanks for your simple explanation

    • @a.yashwanth
      @a.yashwanth 4 ปีที่แล้ว

      who is that.

    • @unknownman1
      @unknownman1 3 ปีที่แล้ว

      @@a.yashwanth kevin naughton jr

  • @salahaosamaelhoseneyali7019
    @salahaosamaelhoseneyali7019 4 ปีที่แล้ว

    great anyway

  • @ompandey8470
    @ompandey8470 2 ปีที่แล้ว

    confusing code and explanation .

  • @HarishAmarnath
    @HarishAmarnath 4 ปีที่แล้ว

    he never used max variable .