【leetcode】815. Bus Routes
题目如下:
We have a list of bus routes. Each
routes[i]is a bus route that the i-th bus repeats forever. For example ifroutes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.We start at bus stop
S(initially not on a bus), and we want to go to bus stopT. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.Example:
Input:
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation:
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.Note:
1 <= routes.length <= 500.1 <= routes[i].length <= 500.0 <= routes[i][j] < 10 ^ 6.
解题思路:对于至少有两条公交线路经过的站点,称为换乘站。如果起点和终点不属于同一公交线路,那么必定要经过换乘站换乘另外的线路。所以对于所有的站点来说,有效的站点只有起点、终点、换乘站。过滤无效站点可以减少计算量,使用BFS即可求得结果。
代码如下:
class Solution(object):
def numBusesToDestination(self, routes, S, T):
"""
:type routes: List[List[int]]
:type S: int
:type T: int
:rtype: int
"""
transfer = {}
for route in routes:
for r in route:
transfer[r] = transfer.setdefault(r,0) + 1
key_list = []
for key in transfer.viewkeys():
key_list.append(key) for key in key_list:
if transfer[key] <= 1:del transfer[key] for i in range(len(routes)-1,-1,-1):
for j in range(len(routes[i])-1,-1,-1):
v = routes[i][j]
if v != S and v != T and v not in transfer:
del routes[i][j] #print routes visit = {}
visit[S] = 0
queue = [(S,0)]
while len(queue) > 0:
stop,count = queue.pop(0)
if stop == T:return count
for route in routes:
if stop not in route:
continue
for r in route:
if r not in visit or visit[r] > count+1:
queue.append((r,count+1))
visit[r] = count+1
return -1
【leetcode】815. Bus Routes的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- [转帖]Java 8新特性探究(八)精简的JRE详解
Java 8新特性探究(八)精简的JRE详解 https://my.oschina.net/benhaile/blog/211804 精简版的api 撸了今年阿里.网易和美团的面试,我有一个重要发 ...
- Chrome的cookie放在哪里了,Cookie/Session机制详解
Chrome的cookie放在哪里了,Cookie/Session机制详解:https://blog.csdn.net/u010002184/article/details/82082951
- Go语言中的数组(九)
我刚接触go语言的数组时,有点不习惯,因为相对于JavaScript这样的动态语言里的数组,go语言的数组写起来有点不爽. 定义数组 go语言定义数组的格式如下: ]int var 数组名 [数组长度 ...
- 【NOIP2017】跳房子
这题我0分. 比赛时,我一眼出正解,哈哈,太水了! 这题不就是一个二分+DP+单调队列吗? 然而,细节决定成败. 我错了许多细节,就挂了. 我只考了0分... 首先,这题满足一个条件: 保证g变大后, ...
- React 中的函数式思想
函数式编程简要概念 函数式编程中一个核心概念之一就是纯函数,如果一个函数满足一下几个条件,就可以认为这个函数是纯函数了: 它是一个函数(废话): 当给定相同的输入(函数的参数)的时候,总是有相同的输出 ...
- model attribute
model attribute,字面意思,给model加attribute以配置数据库 主键 public class OrderDetail { [Key] public int OrderDeta ...
- 9.用ExecuteSqlCommand执行存储过程
比如你有一个存储过程 IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CreateAutho ...
- Git 一般性操作
git全局设定 git config --global user.name “码云账号” git config --global user.email “码云注册邮箱” git 定位文件夹cd进入到需 ...
- JS基础_其他进制的数字(了解)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Shell随机生成字符串
随机生成18位的字符串,数字 大小写字符 斜线 password=`openssl rand -base64 |-`