【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_edges
denotes a red directed edge from nodei
to nodej
. Similarly, each[i, j]
inblue_edges
denotes a blue directed edge from nodei
to nodej
.Return an array
answer
of lengthn
, where eachanswer[X]
is the length of the shortest path from node0
to nodeX
such that the edge colors alternate along the path (or-1
if 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 <= 100
red_edges.length <= 400
blue_edges.length <= 400
red_edges[i].length == blue_edges[i].length == 2
0 <= 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 ...
随机推荐
- Python Module_subprocess_调用 Powershell
目录 目录 前言 Powershell call Python Python call Powershell Powershell发送邮件 最后 前言 使用Python内建的subprocess模块, ...
- python学习笔记:(三)list(列表)常用的内置方法
list(列表)包含一些内置的方法,以下为详细介绍: (方法调用:对象.方法(参数)) 1.append() 在列表的末尾添加新的对象 如: lst=[1,2,3] lst.append(4) --- ...
- GCC 使用-C语言编译过程
任何一种高级语言,要想在机器上执行,必须翻译为机器能读懂的机器语言.编译器就相当于翻译官,将高级语言翻译为机器语言. GCC 最初只用了编译 C 语言程序,全称是 GNU C Compiler.后来扩 ...
- 【web前端】前段时间的面题整理(1)
这是我的试题答案整理,可能有多种答案.我也就写了一两种.在慢慢整合中 第一题 用js实现随机选取10-100之间的10个数字,存入一个数组,去重后求和(保证这10个数字不能出现重复) 要求:去重不能使 ...
- [转帖]TLS握手:回顾1.2、迎接1.3
TLS握手:回顾1.2.迎接1.3 novsec2019-05-10共26541人围观 ,发现 2 个不明物体网络安全 *本文原创作者:novsec,本文属于FreeBuf原创奖励计划,未经许可禁止转 ...
- Spring中的常见注解
@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象. @RestController Spring4之后加入的注解,原来在@Co ...
- java基础笔记(3)
捕获异常: try{ ...... }catch(Exception e){ ...... }finally{ ...... } 注意:在写多重catch时需先小后大: 自定义异常: String字符 ...
- oracle系统视图SQL语句整理
-- DBA/ALL/USER/V_$/GV_$/SESSION/INDEX开头的绝大部分都是视图 -- DBA_TABLES意为DBA拥有的或可以访问的所有的关系表. -- ALL_TABLES意为 ...
- 小白学Python(20)—— Turtle 海龟绘图
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...
- 简述COOKIE和SESSION的区别与联系?
cookie 和session 的区别:1.cookie数据存放在客户的浏览器上,session数据放在服务器上.2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗 ...