{"id":22513976,"url":"https://github.com/emahtab/bus-routes","last_synced_at":"2026-01-07T21:05:22.117Z","repository":{"id":79525329,"uuid":"407756548","full_name":"eMahtab/bus-routes","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2021-09-18T04:45:35.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:26:16.704Z","etag":null,"topics":["breadth-first-search","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-18T04:36:04.000Z","updated_at":"2021-09-18T04:47:20.000Z","dependencies_parsed_at":"2023-05-10T17:15:28.293Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/bus-routes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbus-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbus-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbus-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbus-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/bus-routes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952347,"owners_count":20699472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["breadth-first-search","leetcode"],"created_at":"2024-12-07T03:15:27.614Z","updated_at":"2026-01-07T21:05:22.010Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bus Routes\n## https://leetcode.com/problems/bus-routes\n\nYou are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.\n\nFor example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -\u003e 5 -\u003e 7 -\u003e 1 -\u003e 5 -\u003e 7 -\u003e 1 -\u003e ... forever.\n\nYou will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.\n\nReturn the least number of buses you must take to travel from source to target. Return -1 if it is not possible.\n\n \n```\nExample 1:\n\nInput: routes = [[1,2,7],[3,6,7]], source = 1, target = 6\nOutput: 2\nExplanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.\n\nExample 2:\n\nInput: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12\nOutput: -1\n``` \n\n**Constraints:**\n```\n 1 \u003c= routes.length \u003c= 500.\n 1 \u003c= routes[i].length \u003c= 105\n All the values of routes[i] are unique.\n sum(routes[i].length) \u003c= 105\n 0 \u003c= routes[i][j] \u003c 106\n 0 \u003c= source, target \u003c 106\n```\n\n\n### BFS Implementation 1 : Time Limit Exceeded\n```java\nclass Solution {\n    public int numBusesToDestination(int[][] routes, int source, int target) {\n        if(routes == null || routes.length == 0)\n            return 0;\n        \n        Map\u003cInteger,List\u003cInteger\u003e\u003e stopToRouteMap = new HashMap\u003c\u003e();\n        for(int i = 0; i \u003c routes.length; i++) {\n            for(int j = 0; j \u003c routes[i].length; j++) {\n                stopToRouteMap.putIfAbsent(routes[i][j], new ArrayList\u003cInteger\u003e());\n                stopToRouteMap.get(routes[i][j]).add(i);\n            }\n        }\n        \n        int busChange = 0;\n        Queue\u003cInteger\u003e q = new ArrayDeque\u003c\u003e();\n        q.add(source);\n        \n        Set\u003cInteger\u003e stopSeen = new HashSet\u003c\u003e();\n        stopSeen.add(source);\n        \n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n              int stop = q.remove();\n              if(stop == target)\n                  return busChange;\n              List\u003cInteger\u003e busRoutes = stopToRouteMap.get(stop);\n              for(int routeNumber : busRoutes) {\n                  for(int newStop : routes[routeNumber]) {\n                      if(!stopSeen.contains(newStop)) {\n                          q.add(newStop);\n                          stopSeen.add(newStop);\n                      }\n                  }\n              }  \n            }\n            busChange++;\n        }\n        return -1;\n    }\n}\n```\n\n### BFS Implementation 2 : Optimized\n```java\nclass Solution {\n    public int numBusesToDestination(int[][] routes, int source, int target) {\n        if(routes == null || routes.length == 0)\n            return 0;\n        \n        Map\u003cInteger,List\u003cInteger\u003e\u003e stopToRouteMap = new HashMap\u003c\u003e();\n        for(int i = 0; i \u003c routes.length; i++) {\n            for(int j = 0; j \u003c routes[i].length; j++) {\n                stopToRouteMap.putIfAbsent(routes[i][j], new ArrayList\u003cInteger\u003e());\n                stopToRouteMap.get(routes[i][j]).add(i);\n            }\n        }\n        \n        int busChange = 0;\n        Queue\u003cInteger\u003e q = new ArrayDeque\u003c\u003e();\n        q.add(source);\n        \n        Set\u003cInteger\u003e stopSeen = new HashSet\u003c\u003e();\n        stopSeen.add(source);\n        Set\u003cInteger\u003e routeSeen = new HashSet\u003c\u003e();\n        \n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n              int stop = q.remove();\n              if(stop == target)\n                  return busChange;\n              List\u003cInteger\u003e busRoutes = stopToRouteMap.get(stop);\n              for(int routeNumber : busRoutes) {\n                  if(!routeSeen.contains(routeNumber)) {\n                      for(int newStop : routes[routeNumber]) {\n                        if(!stopSeen.contains(newStop)) {\n                           q.add(newStop);\n                           stopSeen.add(newStop);\n                        }\n                      }\n                      routeSeen.add(routeNumber);\n                  }\n              }  \n            }\n            busChange++;\n        }\n        return -1;\n    }\n}\n```\n\n\n### References :\nhttps://www.youtube.com/watch?v=odmGyOJM5EY (Nice Video)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbus-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbus-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbus-routes/lists"}