{"id":22513963,"url":"https://github.com/emahtab/cheapest-flights-within-k-stops","last_synced_at":"2026-03-19T23:02:53.682Z","repository":{"id":79525348,"uuid":"407783316","full_name":"eMahtab/cheapest-flights-within-k-stops","owner":"eMahtab","description":"Cheapest Flight with k stops","archived":false,"fork":false,"pushed_at":"2024-10-18T17:02:35.000Z","size":2220,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-16T02:42:51.834Z","etag":null,"topics":["dijkstra-algorithm","graph","leetcode"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-18T07:00:38.000Z","updated_at":"2024-10-18T17:02:38.000Z","dependencies_parsed_at":"2025-02-02T03:35:35.173Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/cheapest-flights-within-k-stops","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/cheapest-flights-within-k-stops","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcheapest-flights-within-k-stops","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcheapest-flights-within-k-stops/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcheapest-flights-within-k-stops/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcheapest-flights-within-k-stops/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/cheapest-flights-within-k-stops/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcheapest-flights-within-k-stops/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29010454,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T10:37:29.253Z","status":"ssl_error","status_checked_at":"2026-02-02T10:37:28.644Z","response_time":58,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dijkstra-algorithm","graph","leetcode"],"created_at":"2024-12-07T03:15:17.745Z","updated_at":"2026-02-02T10:37:55.464Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cheapest flights within k stops\n## https://leetcode.com/problems/cheapest-flights-within-k-stops\n\nThere are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.\n\nYou are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.\n\n![\"Cheapest flights within K stops\"](cheapest-flights-within-k-stops-example.jpg?raw=true \"Cheapest flights within K Stops\")\n\nFor the above example the cheapest flight from V0 to V2 with at most 2 stops will be (V0 -\u003e V1 -\u003e V4 -\u003e V2 = 7)\n\n```\nConstraints:\n\n1. 1 \u003c= n \u003c= 100\n2. 0 \u003c= flights.length \u003c= (n * (n - 1) / 2)\n3. flights[i].length == 3\n4. 0 \u003c= fromi, toi \u003c n\n5. fromi != toi\n6. 1 \u003c= pricei \u003c= 104\n7. There will not be any multiple flights between two cities.\n8. 0 \u003c= src, dst, k \u003c n\n9. src != dst\n```\n\n## Implementation : Dijkstra with stops addition, Runtime Complexity = O(N^2 ∗ Log(N))\n```java\nclass Solution {\n    \n    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {\n        int[][] adjMatrix = new int[n][n];\n        for (int[] flight : flights) {\n            adjMatrix[flight[0]][flight[1]] = flight[2];\n        }\n        int[] distances = new int[n];\n        int[] currentStops = new int[n];\n        Arrays.fill(distances, Integer.MAX_VALUE);\n        Arrays.fill(currentStops, Integer.MAX_VALUE);\n        distances[src] = 0;\n        currentStops[src] = 0;\n        \n        // The priority queue will contain (node, cost, stops)\n        PriorityQueue\u003cint[]\u003e minHeap = new PriorityQueue\u003c\u003e((a, b) -\u003e a[1] - b[1]);\n        minHeap.offer(new int[] { src, 0, 0 });\n        while (!minHeap.isEmpty()) {\n            int[] info = minHeap.poll();\n            int node = info[0], cost = info[1], stops = info[2];\n            if (node == dst) {\n                return cost;\n            }\n            if (stops \u003e K) {\n                continue;\n            }\n            for (int nei = 0; nei \u003c n; nei++) {\n                if (adjMatrix[node][nei] \u003e 0) {\n                    int newCost = cost + adjMatrix[node][nei];\n                    // Relaxation based on cost and stops\n                    if (newCost \u003c distances[nei] || stops + 1 \u003c currentStops[nei]) {\n                        minHeap.offer(new int[] { nei, newCost, stops + 1 });\n                        distances[nei] = newCost;\n                        currentStops[nei] = stops + 1;\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n}\n```\n\n### Code execution\n![\"Code Execution\"](code-execution.jpg?raw=true \"Code execution\")\n\n\n## References:\n1. https://leetcode.com/problems/cheapest-flights-within-k-stops/solution\n2. https://leetcode.libaoj.in/cheapest-flights-within-k-stops.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcheapest-flights-within-k-stops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcheapest-flights-within-k-stops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcheapest-flights-within-k-stops/lists"}