Video není dostupné.
Omlouváme se.

Two City Scheduling - Leetcode 1029 - Python

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/two-cit...
    0:00 - Read the problem
    2:07 - Explaining Backtracking
    2:55 - Explaining DP
    5:07 - Explaining Greedy
    9:00 - Coding Greedy
    leetcode 1029
    This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
    #meta #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Komentáře • 40

  • @randomlyasked
    @randomlyasked Před 2 lety +28

    Thanks for doing the daily leetcode challenge. Please don't stop

  • @chaitanyakhot3842
    @chaitanyakhot3842 Před 2 lety +12

    wow this looked so difficult before you explained it. Those logic explanations are so good

  • @gauravdesale47
    @gauravdesale47 Před 2 lety +10

    Nice vid Man I got the oracle cloud services because of your channel

  • @re-think7693
    @re-think7693 Před 2 lety +3

    we can do A-B and then take the first half of array for A and 2nd half for B. That will also work.

  • @xiaohuisun
    @xiaohuisun Před 2 lety +3

    another approach is to think this way: first all would go to city A, and now you have to move one person to city B while keep the total cost to be minimum, what would you do? after the move the cost would be sum(a)-a[k]+b[k] = sum(a)-(a[k]-b[k]), so you would move the person that has the highest difference from b-a. and then so on and so forth.

    • @karanveersingh5535
      @karanveersingh5535 Před 2 lety

      your approach worked like charm with 3ms runtime. Thanks mark.

  • @rumonintokyo
    @rumonintokyo Před rokem +2

    I can always code the solution after I hear your explanation. I never see your code. But getting that thought and approach always stump me and I keep on thinking complicated intuitive ways to solve it.

  • @ygwg6145
    @ygwg6145 Před rokem +1

    Replacing sorting with median finding makes this solution O(n) in time complexity.

  • @k.h.9008
    @k.h.9008 Před 2 lety +2

    Nice, the elegant solution mentioned (in the discussion) has
    for i in range(n // 2):
    minCost += (costs[i][0] + costs[n-i-1][1])
    which can be made more elegant to
    for i in range(n):
    minCost += costs[i][i//(n//2)]

    • @VGBNDGRL
      @VGBNDGRL Před 9 měsíci

      elegant, but so hard to read haha

  • @devinosborne3396
    @devinosborne3396 Před 2 lety +3

    looked very difficult. but i actually understood how to get there!

  • @queueoverflow
    @queueoverflow Před 2 lety +1

    It's great to see the efficient solutions. Today I also solved it though.

  • @mohamedfelofy2270
    @mohamedfelofy2270 Před 2 lety

    Thanks for your effort 😘😘

  • @fdk9913
    @fdk9913 Před 2 lety +1

    No way I literally solved this question this afternoon, and you now solved it. Damn thats such a coincidence.

    • @dataman4503
      @dataman4503 Před 2 lety +2

      since it is 'problem of the day' ;-)

  • @Douglasfaparanhos
    @Douglasfaparanhos Před 2 lety

    Excellent 👏👏👏👏

  • @sidazhong2019
    @sidazhong2019 Před 8 měsíci

    DFS is much easier to understand, with crystal clear logic. The greedy solution is almost impossible to come up with.
    cache={}
    def dfs(k, k1, k2):
    if (k1,k2) in cache:
    return cache[(k1,k2)]
    if k>len(costs)-1:
    return 0
    l=10**10
    r=10**10
    if k1

  • @lavanyam3224
    @lavanyam3224 Před 2 lety +1

    Awesome explanation!! This is a super random qn...can we expect a face reveal? xD

  • @kwakukusi4094
    @kwakukusi4094 Před 2 lety +3

    ha for once, I solved a question before you did 🤣, (I am still going to watch the video though, I want to see if I wrote the most efficient solution)

  • @midhileshmomidi3120
    @midhileshmomidi3120 Před 2 lety

    does using heaps to sort while appending is little better?

  • @orellavie6233
    @orellavie6233 Před 2 lety +1

    I am sure why cache would work with just counts, as countA=1 and countB=1 with two different total received mean different output, which means we not calculate all possibilities, any help?

  • @blasttrash
    @blasttrash Před 2 lety +2

    but how to come up with greedy solution on our own? Whats the natural thought process behind it?

    • @eugeneshcherbo9821
      @eugeneshcherbo9821 Před 2 lety +6

      What's the minimum possible cost regardless of how many people go to A or go to B? The minimum cost for each person, right?
      So, divide people into two groups. Those who have cost of flying to A less than flying to B. These people want to fly to A to save money. And the second group is those who want to fly to B.
      Now we have the minimum possible cost but distribution of people might be wrong.
      So if the groups turned out to have n people each, then we are done.
      If some group is larger than another, think how do you move people from this larger group so that each group has n people? Since now you have the minimum possible cost, you want to move people in the way that minimize the additional cost.
      Let's imagine that group B is larger. Then to minimize additional cost we would like to move those people to group A who will suffer the least by moving to flight to A. Or in other words we want to minimize additional cost from moving a person to group A and maximize the cost reduction in group B. Thus we tend to search people with cost of flying to A as little as possible and cost of flying to B as much as possible.
      Looking carefully at it shows that it is the same as just sort the differences.

    • @VGBNDGRL
      @VGBNDGRL Před 9 měsíci

      @@eugeneshcherbo9821 thank you so much for that breakdown, very helpful in building the intuition

  • @krateskim4169
    @krateskim4169 Před 2 lety

    Great Great Great

  • @chandramoulidasari3946

    Sir can you suggest me please where i can learn DS in python .

  • @coreanoquant
    @coreanoquant Před 2 lety +1

    So I'm a Computer Science student. I can answer the problems mathematically but actually implementing it in code is a very difficult task for me. Maybe it's because I only just started doing leetcode questions and I don't do coding unless I'm doing university work. Anyone have the same problem?

    • @VGBNDGRL
      @VGBNDGRL Před 9 měsíci

      Yes, and tbh, everyone has the same problem when they start Leetcoding. You're applying both problem solving techniques AND language syntax. It isn't easy. It's like being a baby and being told that you need to understand grammar at the same time you are learning vocabulary. You can't just know these things from zero. You build intuition for the problem, and then slowly code it. The more you do it, the faster you will get at coding your solutions from scratch.

    • @sidazhong2019
      @sidazhong2019 Před 8 měsíci

      You can learn fancy tech skills in weeks. But solving leet code problems took years of practice.

  • @affafa100
    @affafa100 Před rokem

    Can someone please explain why DP solution had complexity O(n*n).

  • @name_surname_1337
    @name_surname_1337 Před 9 měsíci

    This solution is not intuitive at all; this type of sorting is very tricky. It's much easier to sort the costs by absolute difference and then, while both cities are not equal N, fill them with minimums. Once one city is full (equal to N), put everything into the second city

  • @user-ps8ov4yh5v
    @user-ps8ov4yh5v Před 2 lety +1

    Well preview is not actually accurate. Based on data from leetcode this question is asked only by Bloomberg in the last 0-6 months

    • @subinnair3835
      @subinnair3835 Před 2 lety +1

      Its Accurate !
      Check in 6months-1year category

    • @user-ps8ov4yh5v
      @user-ps8ov4yh5v Před 2 lety +1

      @@subinnair3835 and I see 2 Amazon and 2 Facebook. You cant say its FB(Meta) question if it was asked 26 times by Bloomberg and only 2 times by FB in a year

    • @vour6654
      @vour6654 Před 2 lety

      @@user-ps8ov4yh5v I mean Meta did ask it. It would be proper to put Bloomberg however.

  • @SmoothCode
    @SmoothCode Před 2 lety +1

    Can I plz see the alternative way to solve it using DP in code form? Thanks.

  • @masternobody1896
    @masternobody1896 Před 2 lety

    First

  • @prabhatracherla3098
    @prabhatracherla3098 Před 2 lety

    I was asked this question in one of the interview. I couldn't solve it. Because its really HARD. Just because leetcode marks them as medium it doesn't need to be medium. And interviewers grade you thinking that you can't solve medium also. So its a reject. Honestly, what's the point of these question in real world use case. Which company is running short of budjet to send employee overseas so that they can make millions for the company only. These questions are so useless

  • @SRoyGardening
    @SRoyGardening Před 2 lety

    It's great to see the efficient solutions. Today I also solved it though.