Two City Scheduling - Leetcode 1029 - Python

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

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

  • @randomlyasked
    @randomlyasked 2 ปีที่แล้ว +30

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

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

      He’s a beast

  • @chaitanyakhot3842
    @chaitanyakhot3842 2 ปีที่แล้ว +16

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

  • @gauravdesale47
    @gauravdesale47 2 ปีที่แล้ว +11

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

  • @xiaohuisun
    @xiaohuisun 2 ปีที่แล้ว +5

    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 2 ปีที่แล้ว

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

  • @re-think7693
    @re-think7693 2 ปีที่แล้ว +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.

  • @sidazhong2019
    @sidazhong2019 11 หลายเดือนก่อน

    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

  • @rumonintokyo
    @rumonintokyo ปีที่แล้ว +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 ปีที่แล้ว +1

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

  • @k.h.9008
    @k.h.9008 2 ปีที่แล้ว +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 ปีที่แล้ว

      elegant, but so hard to read haha

  • @devinosborne3396
    @devinosborne3396 2 ปีที่แล้ว +3

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

  • @orellavie6233
    @orellavie6233 2 ปีที่แล้ว +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?

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

    does using heaps to sort while appending is little better?

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

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

    • @dataman4503
      @dataman4503 2 ปีที่แล้ว +3

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

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

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

    • @eugeneshcherbo9821
      @eugeneshcherbo9821 2 ปีที่แล้ว +7

      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 ปีที่แล้ว

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

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

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

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

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

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

    Thanks for your effort 😘😘

  • @kwakukusi4094
    @kwakukusi4094 2 ปีที่แล้ว +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)

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

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

  • @coreanoquant
    @coreanoquant 2 ปีที่แล้ว +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 ปีที่แล้ว

      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 11 หลายเดือนก่อน

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

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

    Excellent 👏👏👏👏

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

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

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

    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

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

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

  • @Code8-o4c
    @Code8-o4c 2 ปีที่แล้ว +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 2 ปีที่แล้ว +1

      Its Accurate !
      Check in 6months-1year category

    • @Code8-o4c
      @Code8-o4c 2 ปีที่แล้ว +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 2 ปีที่แล้ว

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

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

    Great Great Great

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

    First

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

    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

  • @techandmore12
    @techandmore12 หลายเดือนก่อน

    Min Heap:
    def twoCitySchedCost(self, costs: List[List[int]]) -> int:
    minH = []
    for i, cost in enumerate(costs):
    heapq.heappush(minH, (cost[0] - cost[1], i))
    res = 0
    for _ in range(len(costs) // 2):
    index = heapq.heappop(minH)[1]
    res += costs[index][0]
    for _ in range(len(costs) // 2):
    index = heapq.heappop(minH)[1]
    res += costs[index][1]
    return res

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

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