Cheapest Flights Within K Stops | LeetCode 787 | C++, Java, Python

Sdílet
Vložit
  • čas přidán 13. 06. 2020
  • LeetCode Solutions: • LeetCode Solutions | L...
    June LeetCoding Challenge: • Playlist
    May LeetCoding Challenge: • Playlist
    *** Best Books For Data Structures & Algorithms for Interviews:*********
    1. Cracking the Coding Interview: amzn.to/2WeO3eO
    2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
    3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
    4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
    5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
    6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
    *****************************************************************************
    June LeetCoding Challenge | Problem 14 | Cheapest Flights Within K Stops | 14 June,
    Facebook Coding Interview question,
    google coding interview question,
    leetcode,
    Cheapest Flights Within K Stops,
    Cheapest Flights Within K Stops c++,
    Cheapest Flights Within K Stops Java,
    Cheapest Flights Within K Stops python,
    Cheapest Flights Within K Stops solution,
    787. Cheapest Flights Within K Stops,
    dijkstra algorithm,
    #Facebook #CodingInterview #LeetCode #JuneLeetCodingChallenge #Google #Amazon #ShortestPath #Dijkstra

Komentáře • 47

  • @ritikrao1782
    @ritikrao1782 Před 2 lety +5

    it gives TLE on leetcode
    this method is not correct for larger inputs

  • @manavneekhra4901
    @manavneekhra4901 Před 2 lety +5

    leetcode doesn't accept this solution .they must have updated test cases.

  • @manpreetkhokhar5318
    @manpreetkhokhar5318 Před 4 lety +1

    I am learning a lot from this channel. Thanks. Keep it coming.I solved it by DP method.

  • @anantripathi5117
    @anantripathi5117 Před 3 lety +4

    I applied your solution and it failed and gave tle i think the test cases have been updated you need to update your code too

  • @xapeuuu
    @xapeuuu Před 4 lety +1

    Very nice and clean solution!

    • @KnowledgeCenter
      @KnowledgeCenter  Před 4 lety

      Thanks!

    • @xapeuuu
      @xapeuuu Před 4 lety

      @@KnowledgeCenter interesting on how in this problem, we don't update the adj matrix/list at all and keep all the results inside the priority queue ^^

  • @ChristianESL
    @ChristianESL Před 4 lety +1

    Thanks for the video. I was able to solve with java.

  • @bhavuks6554
    @bhavuks6554 Před 4 lety

    at 20:59 seeing 'runtime error: reference binding to null pointer of type'
    I could not understand why adjacency list was fixed to a pre-allocated size (n)?

    • @KnowledgeCenter
      @KnowledgeCenter  Před 4 lety

      our vector was empty, and we were trying to assign values to it using adj[idx] = ...
      over the next line, while building it from flights vector.

    • @bhavuks6554
      @bhavuks6554 Před 4 lety

      @@KnowledgeCenter Oh that makes sense, thank you for replying, your videos have been helpful in understanding the daily challenge problems on LC. Thanks again :)

  • @nagarjunvinukonda162
    @nagarjunvinukonda162 Před rokem

    Leetcode few test cases don't run for this solution. It says time limit exceeded

  • @jayaprakashreddythokala1760

    why r u not finalising when u pop B 8/2

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

    this won't work. really bad time complexity also no cycle avoidance

  • @FenixFahad
    @FenixFahad Před 4 lety +1

    Very nice👌

  • @KnowledgeCenter
    @KnowledgeCenter  Před 4 lety

    Java Code: (Thanks @Srikant Sharma for sharing):
    ------------------------------------------------------
    class Solution {
    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
    List graph = new ArrayList();
    //creating adjacency list for source cities.
    for (int i = 0; i < n; i++)
    graph.add(new ArrayList());
    for (int[] flight : flights) {
    //source city: [destination city, source to destination cost].
    graph.get(flight[0]).add(new int[]{flight[1], flight[2]});
    }
    //MinHeap: input format: [city, distance, cost], it compares based on cost.
    PriorityQueue minHeap = new PriorityQueue((a, b) -> a[2] - b[2]);
    minHeap.add(new int[]{src, 0, 0});
    while (!minHeap.isEmpty()) {
    int[] cur = minHeap.poll();
    int city = cur[0], distance = cur[1], cost = cur[2];
    if (city == dst)
    return cost;
    if (distance

  • @khorshedalam5860
    @khorshedalam5860 Před 4 lety +1

    I've confusion, while updating adjacent node, should we check it's previous cost with and cost by current node and update only if cost by current node is minimum?

    • @khorshedalam5860
      @khorshedalam5860 Před 4 lety +1

      got it from video explanation, same node kept multiple time with different hop number

  • @indiasuhail
    @indiasuhail Před rokem

    Atleast the python code fails on leetcode. Timeout error.

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

    This is giving tle

  • @shivanshutiwari5646
    @shivanshutiwari5646 Před 4 lety

    where did you min amount??

    • @Ash-fo4qs
      @Ash-fo4qs Před 2 lety

      here priority queue is used ( min heap ), so when u take the top element from the queue--u always get the minimum element based on distance.

  • @nitinnanda1865
    @nitinnanda1865 Před 4 lety

    You mentioned Java in the video title but didn't include it in the video !!

    • @skmn07
      @skmn07 Před 3 lety

      leetcode.com/problems/cheapest-flights-within-k-stops/discuss/115541/JavaPython-Priority-Queue-Solution

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

    Wrong Solution Again, Will get into an infinite loop in case of directed cycle...! Just because leetcode used weak test cases for this question, CZcams is filled with wrong solution....!!!!!

    • @harshpeswani4187
      @harshpeswani4187 Před 2 lety

      I think it will not get into infinite loop as the stops value is also increasing so eventually it will come out of the cycle because of less than k condition. Time complexity is the issue in this solution.

  • @anandmishra8060
    @anandmishra8060 Před 4 lety +2

    why not in java?

    • @KnowledgeCenter
      @KnowledgeCenter  Před 4 lety +3

      Took a shortcut for Shortest Path. 😀

    • @srikantsharma6430
      @srikantsharma6430 Před 4 lety +5

      Please find below the java solution.
      class Solution {
      public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
      List graph = new ArrayList();
      //creating adjacency list for source cities.
      for (int i = 0; i < n; i++) {
      graph.add(new ArrayList());
      }
      for (int[] flight : flights) {
      //source city: [destination city, source to destination cost].
      graph.get(flight[0]).add(new int[]{flight[1], flight[2]});
      }
      //MinHeap: input format: [city, distance, cost], it compares based on cost.
      PriorityQueue minHeap = new PriorityQueue((a, b) -> a[2] - b[2]);
      minHeap.add(new int[]{src, 0, 0});
      while (!minHeap.isEmpty()) {
      int[] cur = minHeap.poll();
      int city = cur[0], distance = cur[1], cost = cur[2];
      if (city == dst) {
      return cost;
      }
      if (distance

  • @VinayKumar-pi4wm
    @VinayKumar-pi4wm Před 2 lety

    solution gives tle

  • @JohnTosun
    @JohnTosun Před 3 lety +1

    Language doesnt matter but that is not a good explanation. U are mixing dijkstra and some of your thoughts

  • @priyakolluru356
    @priyakolluru356 Před 3 lety

    Why didn't you take visited array? Neighbours of D is B and E. Where are you marking visited? If you do not need to take visited, why don't we?

  • @idriskas
    @idriskas Před 4 lety +1

    you didnt do it in java

  • @gaunikasrivastava7851
    @gaunikasrivastava7851 Před 3 lety +1

    Hella slow explanation

  • @pulkitxd
    @pulkitxd Před 3 lety +1

    Painstakingly Slow Explanation