【leetcode】1129. Shortest Path with Alternating Colors
题目如下:
Consider a directed graph, with nodes labelled
0, 1, ..., n-1. In this graph, each edge is either red or blue, and there could be self-edges or parallel edges.Each
[i, j]inred_edgesdenotes a red directed edge from nodeito nodej. Similarly, each[i, j]inblue_edgesdenotes a blue directed edge from nodeito nodej.Return an array
answerof lengthn, where eachanswer[X]is the length of the shortest path from node0to nodeXsuch that the edge colors alternate along the path (or-1if such a path doesn't exist).Example 1:
Input: n = 3, red_edges = [[0,1],[1,2]], blue_edges = []
Output: [0,1,-1]Example 2:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[2,1]]
Output: [0,1,-1]Example 3:
Input: n = 3, red_edges = [[1,0]], blue_edges = [[2,1]]
Output: [0,-1,-1]Example 4:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[1,2]]
Output: [0,1,2]Example 5:
Input: n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]
Output: [0,1,1]Constraints:
1 <= n <= 100red_edges.length <= 400blue_edges.length <= 400red_edges[i].length == blue_edges[i].length == 20 <= red_edges[i][j], blue_edges[i][j] < n
解题思路:本题采用BFS的思想。对于每一个节点来说,分别求出其红边和蓝边作为入口的最小值。
代码如下:
class Solution(object):
def shortestAlternatingPaths(self, n, red_edges, blue_edges):
"""
:type n: int
:type red_edges: List[List[int]]
:type blue_edges: List[List[int]]
:rtype: List[int]
"""
res = [0] + [float('inf')] * (n - 1)
queue = []
red_used = [0] * len(red_edges)
blue_used = [0] * len(blue_edges)
def process(target, edges, res, color,used_list,step_count):
for inx,(i, j) in enumerate(edges):
used = used_list[inx]
if i == target and used == 0:
res[j] = min(res[j],step_count + 1)
queue.append((j, color,step_count + 1))
used_list[inx] = 1
#red
process(0, red_edges, res, 'R',red_used,0)
while len(queue) > 0:
num, color,step = queue.pop(0)
if color == 'R':
process(num, blue_edges, res, 'B',blue_used,step)
else:
process(num, red_edges, res, 'R',red_used,step) red_used = [0] * len(red_edges)
blue_used = [0] * len(blue_edges)
process(0, blue_edges, res, 'B', blue_used,0)
while len(queue) > 0:
num, color,step = queue.pop(0)
if color == 'R':
process(num, blue_edges, res, 'B',blue_used,step)
else:
process(num, red_edges, res, 'R',red_used,step) res = map(lambda x: x if x != float('inf') else -1, res)
return res
【leetcode】1129. Shortest Path with Alternating Colors的更多相关文章
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 1129. Shortest Path with Alternating Colors
原题链接在这里:https://leetcode.com/problems/shortest-path-with-alternating-colors/ 题目: Consider a directed ...
- 【leetcode】1091. Shortest Path in Binary Matrix
题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
随机推荐
- webpack bundle中parentJsonpFunction的作用
parentJsonpFunction作用:使异步加载的模块在多个不同的bundle内同步. 假设有多入口文件 bundle1.js: bundl2.js: 在webpack打包后 加载流程: 1.b ...
- SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置
转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...
- 算法初级(scala)
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum 1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为 ...
- Cocos2d-X网络编程(1) 网络基本概念
网络模型 OSI层模型.TCP/IP的层模型如下所示. TCP/IP各层对应的协议如下所示. 通过初步的了解,我知道: IP协议:对应于网络层,是网络层的协议, TCP协议:对应于传输层,是传输层的协 ...
- pandas DataFram的insert函数
原文链接:https://blog.csdn.net/yanwucao/article/details/80211984 DataFrame.insert(loc, column, value, al ...
- Java中类和接口
很形象的接口的使用——针对初学者 里氏代换原则是什么?听起来很高深,不过我们也不是什么学院派,就不讲大道理了,直接拿个例子来说一下. 我们拿人和程序员举个例子.人是一个大类,程序员是继承自人的子类.看 ...
- 20191112 Spring Boot官方文档学习(4.3)
4.3.Profiles Spring Profiles提供了一种隔离部分应用程序配置并使之仅在某些环境中可用的方法.任何@Component,@Configuration或@Configuratio ...
- STL 函数适配器(function adapter)
函数适配器(function adapter):通过不同函数适配器的绑定,组合和修饰能力,可以实现强大的功能,配合STL泛型算法完成复杂功能. 绑定(bind) template <class ...
- 第十一周总结 继承、this和super的区别和用法、方法的重写和重载
一.继承 1.子类继承父类,通过一个关键字 extends 2.子类的对象可以调用父类中的(public protected)属性和方法 当作自己的来使用 3.子类可以添加自己独有的属性和方法 4.子 ...
- [Python3] 042 日志
目录 LOG 1. 日志相关概念 1.1 日志的级别 level 1.2 LOG 的作用 1.3 日志信息 1.4 成熟的第三方日志 1.5 注意 2. Logging 模块 2.1 日志级别 2.2 ...