Reconstruct Itinerary | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • This video explains an important graph programming interview problem which is to reconstruct itinerary.This problem can be solved by using just simple graph traversal technique by using multiset and stack.Map is used to construct the adjacency list because we need random access by string value.Multiset is used to keep the values sorted in ascending order.Stack is used to handle the route exceptions.I have explained the entire problem step by step by using proper examples and intuition for each step.I have dry run the algorithm and have also explained the code walk through at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    =================================================================
    INSTAGRAM: / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    =================================================================
    CODE LINK: gist.github.co...
    USEFUL LINKs:-
    Representation of graph using adjacency matrix and adjacency list: • Representation of grap...
    DFS: • Depth first search | D...
    BFS: • Breadth first search |...

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

  • @subham-raj
    @subham-raj 4 ปีที่แล้ว +12

    How can this be O(E)? How can the removal of edge from the adjacency list (sorted by value) be O(1)?

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

      Insertion & Deletion is logN in a multiset.So,if you consider all the insertions and deletions, it will be O(ElogE).

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

      Amortized complexity is O(1) and worst case complexity is O(log(size_of_ds))

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

      Yea correct.

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

      you can use priority queue instead of multiset .if you use priority queue then you are removed first element which is O(N)

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

    This stack DS in this problem was really not intuitive. Good one!

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

    Your videos are so good! I love the way you break down problems and give such clear concise answers. Thank you so much!

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

    much better than the leetcode solution.. I attempted by using recursion and loop through adj nodes and tried to keep track of which ones ive visited but keep encountering edge case errors... learned something new with stacks today

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

    Love the thought process when using a stack

  • @AnshulSharma-vw9wu
    @AnshulSharma-vw9wu 4 ปีที่แล้ว +3

    HI @Techdose
    I am a Java developer , so most of the times , i don't understand fully your C++ code , but i liked your thought process , so whenever i stuck , i came to your channel , i am preparing for my upcoming interview . This problem seems to be tricky DFS ( Post order Traversal problem ) .
    /**
    [
    ["JFK","SFO"],
    ["JFK","ATL"],
    ["SFO","ATL"],
    ["ATL","JFK"],
    ["ATL","SFO"]
    ]



    JFK= [SFO,ATL] , SFO=[ATL] ,ATL =[JFK,SFO]

    1. Prepare the adjList = JFK= [SFO,ATL] , SFO=[ATL] ,ATL =[JFK,SFO].
    2. Then do DFS for each neighbours . Only trick is do Post order where neighbours goes first and then actual Node.
    3. Since the answer is in reverse Order . Then reverse at last.
    NOTE : I have used PriorityQueue for maintaining sorted order in Adjacency list.

    dfs
    Iteration Answer
    start = JFK answer [SFO,ATL,SFO,JFK, ATL,JFK]

    ATL answer [SFO,ATL,SFO,JFK, ATL]

    JFK answer [SFO,ATL,SFO,JFK]

    SFO answer [SFO,ATL,SFO]

    ATL answer [SFO,ATL]

    SFO answer [SFO] --- started populating from here since it is recursion
    **/
    public List findItinerary(List tickets) {
    List answer = new ArrayList();
    if(tickets== null|| tickets.isEmpty()){
    return answer;
    }
    Map adjList = new HashMap();
    for(List ticket : tickets){
    if(!adjList.containsKey(ticket.get(0))){
    adjList.put(ticket.get(0),new PriorityQueue());
    }
    adjList.get(ticket.get(0)).offer(ticket.get(1));
    }
    dfs("JFK",adjList,answer);
    Collections.reverse(answer);
    return answer;
    }

    private void dfs(String node,Map adjList,List answer) {
    PriorityQueue neighbours = adjList.get(node);
    if(neighbours != null && !neighbours.isEmpty()){
    int size = neighbours.size();
    while(!neighbours.isEmpty()){
    String neigh = neighbours.poll();
    dfs(neigh,adjList,answer);
    }
    }
    answer.add(node);
    }

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

    Thanks for explaining. It helps immensely if you can also talk about intuition behind why the chosen logic would work. Cheers!

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

    Great explanation Thanks! The process of Adding to stack -> popping the elements -> reversing them before returning answer; instead of that can we use queue, we can reduce the cost of reversing the answer array ?

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

    Mad respect for you bro, the way you broke down the problem and simplified it

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

    This video was so helpful! It totally cleared up adjacency lists and stacks for me.
    Thank you!

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

    This solution is Brutally AWESOME!!!

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

    Make July challenge videos also. It would be really helpful to learn from you!Dont stop :)

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

      I will make graph algorithms. Lately, a lot of graph questions are coming on leetcode. It will be better to cover the basics first :)

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

      @@techdose4u bhaiya please make graph videos . U really understands well :)

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

      @@techdose4u If you do explain a graph algorithm, it would be great if you link an example problem on leetcode, so we can practise :)

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

      Yea that's a great idea :)

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

    he makes every problem so simple

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

    please include live coding at the end of the video...your explaination are awesome but when i start to code m stuck on who to implement it.

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

      It doesn't make sense to code again because it will take a lot of time to code and if any error comes during recording then that will be painful as well.Having already written code reduces the time of video as well. It's better to just listen to the idea and then you can have a peek at the code and then try implementing it. It should be fun. If you are stuck then have a look at the code again.

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

    You are literally god

  • @b.jayanagavarmavarma191
    @b.jayanagavarmavarma191 ปีที่แล้ว

    I injected your technical dose in my brain (techdose) . this feels amazingggggggggg....

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

    How did you land on the thought that stack would give the right answer?

  • @MuntasirHossen-w3s
    @MuntasirHossen-w3s ปีที่แล้ว

    @Techdose explanations are awesome

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

    Can we use set in place of multiset?

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

      No you cannot in this problem because there may b multiple flights from one city to anothr city

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

    if you are feel akward using multiset then just use priority queue

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

    Extremely well explained

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

    Great Explanation
    Sep'14, 2023 06:39 pm

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

    Can't be great anymore, genius!

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

    Thanks for explanation. Can you let me know the software used in making the screens and video

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

    sir why u have used multiset instead of sets as we cannot have more than 1 ticket between same source and destination or can we have?? i think its not mentioned in the question that's why u have taken multiset na otherwise u would have gone with sets right??

    • @ShreyaSingh-vr9qi
      @ShreyaSingh-vr9qi 4 ปีที่แล้ว +1

      Ho sakta hain, check this test case
      [["EZE","AXA"],["TIA","ANU"],["ANU","JFK"],["JFK","ANU"],["ANU","EZE"],["TIA","ANU"],["AXA","TIA"],["TIA","JFK"],["ANU","TIA"],["JFK","TIA"]]

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

      Cyclic route will have same destinations multiple times.

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

    so its a topological sort using dfs?

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

    Very helpful explanation. Thanks for even covering the edge cases 👍

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

    can you explain why we choose the adjacency list instead of the adjacency matrix and how we can think of using stack?

  • @SHIVAMRANA-xn8zg
    @SHIVAMRANA-xn8zg 2 หลายเดือนก่อน

    Great Explanation

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

    Sir plzzz make lectures in which u cover all the topics of data structures..
    there are many lectures in youtube but kisi ka code nhi diya hai .... it will be really helpful if we code using your logic

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

      I dint get your question. I think you need me to cover all DS with cod right? So, yes i will make organized lectures from July. Starting from GRAPH then DP and then rest.

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

      @@techdose4u yes sir ... it will be helpful

  • @RahulRaj-rq4lh
    @RahulRaj-rq4lh 2 ปีที่แล้ว

    Wow , learned new things in video thanks

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

    Such a brilliant explanation ☺️😊.. thank you sir..

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

    Thanks a lot for your videos, mate. They're very helpful.

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

    What is the intuition behind using stack? How do we derive the idea of using a stack? Can anyone shed some light here?

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

      It's DFS graph traversal which uses a stack

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

    how you thought of stack🤔
    btw great explanation🙏🙏

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

    very nicely explained. thanks a ton!

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

    i did it same thing using recursion ....and also used priority queue to keep adjacency list sorted in the map .

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

      Sir , which is better acc to you /// multiset or priority queue??

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

      Both have the same complexity. So anything is fine.

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

      @@techdose4u ok

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

    Best explanation !!!

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

    what a nice explanation

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

    Very beautiful explanation.

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

    We can solve this question without explicitly using stack through DFS. Recursion basically is implemented here with a stack.

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

    absolutely amazing explanation!!

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

    I have a doubt brother ,why multiset and not set? any test case for the explanation??..P.s: Your videos are great.

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

    Can you please check your playlists once, so many videos are repetitive. Check DP videos - Perfect squares, coin change 2. Please organize the playlists.

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

      Yes will do it. Its youtube error :(

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

    but if we are using dfs then even if there is a dead end then it will get back to parent node ....why will the execution stop if dead end is there

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

      +1

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

      but there was no route from dead end to parent node , there was not any edge from dead end to parent , queesitons says you have travel if there is an edge

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

      @@sidharthdhiman4522 but this can be implemented using dfs(recursive)

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

    Using that stack was , just 💥

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

    How did u arrive at conclusion to use a stack ?

  • @Sky-nt1hy
    @Sky-nt1hy 3 ปีที่แล้ว +1

    Hi!! thank you for your amazing lecture as always.
    I'm using c++11 on vscode but for some reason multiset STL doesn't work on my environment.
    Is there any way to handle with that?
    Thank you!

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

      Try some other version of CPP like CPP-17 etc.

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

    Please tell me why stack is helping ? i know its giving the correct answer what is intuition behind it so that in future in can think of stack if this kind of problem comes

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

      We are doing DFS. It's tricky though. We need to take care of the condition which I showed you. We travel to a lexically smaller node but it doesn't have the return path. If you take a stack then it will be popped first and hence will be at last in our answer array. That's the idea for using stack.I showed the example in video. I got that after getting WA on the same test case.

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

      I have the same question..

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

      @@techdose4u i did.everything just stack part didnt came into my mind i got WA for returning component part...thankyou so much

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

      I got stack idea from the WA which I showed in the video. Choosing the wrong path.

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

    I don't think interviewers will let you use the multiset Guava library in the interviews :/

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

    such a well and clear approach..nice

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

    Code is the gist might have an extra while loop in if condition. Please check.

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

      Yea it was the incorrect version which I was trying. I have updated with the correct version.

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

    hi sir i need one help from you side
    when i click on solution tab in leet code it shows u have to subscribe
    sir is it not free, sir whether I have to pay for that ..

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

      Some solutions on leetcode are locked.

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

    What is the intuition behind taking stack DS

  • @TomJerry-bp9ig
    @TomJerry-bp9ig ปีที่แล้ว

    Awesome explanation brother ❤

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

    Nice

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

    Brilliant

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

    what's the intuition behind this logic?

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

    Iam getting TLE with this approach

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

    Awesome !!!

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

    please provide link of this question.. on gfg.. also.. 😄..

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

      This is from leetcode I guess

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

      @@techdose4u yesss.., i didn't find it on.. gfg..
      but thanks for your videos..! i start coding 6 November 2020.. and i score.. 650 .. in 3 months.. 😄

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

    Great. But It would have been better if you had explained the intution behind taking the stack. You have just simply used it. But the intuition of using stack is not clear.

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

    those who don't want to use multi set,We can sort the array in reverse order, as popping the first element is a costly operation

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

    Sir can you please tell us that How we got to know that which data structure should be used for such problems. Because When I try this type of question by my own then it took a long time to get some observation and logic behind it...

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

      One more thing is how much time it takes you to do this type of questions to you if it is completely new question to you ????

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

      It depends. I got 2 WA before I submitted the correct version. Took 40 mins to solve this. It was slightly tricky. I was using map + vector. Then moved to Map+ multiset. After that I got WA and took that test case and used stack to finally get it correct. So, it was not that simple to solve at one shot because problem was not clear.

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

    Intuition behind using stack, is not explained properly :(

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

    How to get the intuition of using set?

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

    cant topological sort be used?

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

    Python Code :
    from collections import defaultdict
    matrix = defaultdict(lambda: [])
    for i in tickets:
    matrix[i[0]].append(i[1])
    for i in matrix:
    matrix[i].sort(reverse=True)
    ans = []
    def dfs(i):
    if matrix[i]:
    v = matrix[i].pop()
    dfs(v)
    if matrix[i]:
    dfs(i)
    else:
    ans.append(i)
    else:
    ans.append(i)
    dfs('JFK')
    print(ans)

  • @AnshulSharma-vw9wu
    @AnshulSharma-vw9wu 4 ปีที่แล้ว +1

    Hi Buddy , it seems like your code is only working for 18 out of 80 test cases
    One such EXample is [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]] the correct out put is ["JFK","NRT","JFK","KUL"]

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

      Updated now. That was the wrong version which I shared.

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

    class Solution {
    public:
    vector findItinerary(vector& tickets)
    {
    for (vectorticket : tickets)
    targets[ticket[0]].insert(ticket[1]);
    visit("JFK");
    return vector(route.rbegin(), route.rend());
    }
    map targets;
    vector route;
    void visit(string airport)
    {
    while (targets[airport].size())
    {
    string next = *targets[airport].begin();
    targets[airport].erase(targets[airport].begin());
    visit(next);
    }
    route.push_back(airport);
    }
    };

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

    Similar to leetcode course schedule ii

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

    could not understand intuition behind using stack

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

      Stack is used for backtracking. So that when we backtrack from a path then we simply pop stack elements to get traversed nodes from fest to source order.

  • @Karan-vq5vg
    @Karan-vq5vg 4 ปีที่แล้ว +1

    The answer is coming out wrong after submission please check the code. Only 18 out of 80 cases are getting passed

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

      Code is fine Karan. I also used same logic

    • @Karan-vq5vg
      @Karan-vq5vg 4 ปีที่แล้ว

      @@spetsnaz_2 idk why my test cases are getting failed. I guess i should've tweak the code a bit.

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

      @@Karan-vq5vg Yes 18/80 cases are passing

    • @AbhishekKumar-hj4qo
      @AbhishekKumar-hj4qo 4 ปีที่แล้ว

      yes, the code is partially accepted. TECH DOSE Sir please correct it!

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

      The problem with the code is the second while statement. After you reach an edge with size = 0 you need to pop only that edge and continue with the code. The "while" there was removing every edge as soon as an edge with size == 0 was found.

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

    Smart Guy

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

    EYE TEA NERR E ( British. ) -> For fellow coders who might find it difficult to pronounce

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

    You placed N sticks on a straight line. The i th stick is located at position xi and has height hi. There are no two sticks located at the same position.
    You can push sticks to make them fall to the left or the right side. While falling, a stick can touch its neighbors and make them to fall in the same direction.
    If a stick at position x with height h is falling to the left, then it makes all the sticks at positions (x - h)....x inclusive fall on to the left.
    If a stick is falling to the right, it makes all the sticks at positions x...(x + h) inclusive to fall to the right.
    Your task is to determine the minimal number of sticks you are required to push to make all N sticks fall.
    ex :
    5 sticks
    1 2
    3 1
    6 2
    7 1
    9 2
    ans : 2
    note:
    1 , 2 indicates a stick is placed at x = 1 with a height h = 2
    Sir, can you help me with this.

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

      Where did you get the question?

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

      @@BarkaDog A friend of mine asked me if I can solve this.

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

      Hi, this is an interesting question, first you will have to form 2 graphs assuming only left falls are allowed in first graph and only right falls for the second one. Then, you can find vertices with indegree zero in both graphs, these are vertices that must fall in case only left or right falls are allowed. These vertices will cover the whole distance in which all sticks are placed. Now, keeping the intervals of vertex covers for both graphs side by side, you need to choose minimum number of intervals that cover the whole distance in which the sticks are placed, this can easily be done by starting to pick interval from both ends of distances greedily by choosing larger interval from the ones having same start or end point.

  • @YouTube-Joy-Happy
    @YouTube-Joy-Happy 4 ปีที่แล้ว

    Nice one

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

    topo sort, it is

  • @NikhilSingh-hv9cm
    @NikhilSingh-hv9cm 4 ปีที่แล้ว

    to select the starting location!!!!!!!?

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

    Sir, you github link is not working

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

    I completely lost you on that stack part. You didnt explain the intuition behind it.

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

    😀

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

    Python recursive solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
    res = ["JFK"]
    tickets.sort()
    adj = collections.defaultdict(list)
    for src, dst in tickets:
    adj[src].append(dst)
    if dst not in adj:
    adj[dst] = []
    output = []
    stack = ["JFK"]

    def dfs(src):
    while adj[src]:
    nei = adj[src].pop(0)
    stack.append(nei)
    dfs(nei)
    if stack:
    stack.pop()
    output.append(src)
    dfs("JFK")
    return output[::-1]

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

    Very well explained :)
    I tried to do without stack got TLE!
    class Solution {
    public:
    vector findItinerary(vector& tic) {
    unordered_map m;
    for(int i=0;i

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

    Code Link : techdose.co.in/reconstruct-itinerary-leetcode-332/

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

    How do you decide that your source will be JFK?
    I dont think you can generalize this.

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

    Waiting for your video bhaiya 😅