【leetcode】924.Minimize Malware Spread
题目如下:
In a network of nodes, each node
i
is directly connected to another nodej
if and only ifgraph[i][j] = 1
.Some nodes
initial
are initially infected by malware. Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.Suppose
M(initial)
is the final number of nodes infected with malware in the entire network, after the spread of malware stops.We will remove one node from the initial list. Return the node that if removed, would minimize
M(initial)
. If multiple nodes could be removed to minimizeM(initial)
, return such a node with the smallest index.Note that if a node was removed from the
initial
list of infected nodes, it may still be infected later as a result of the malware spread.Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0Example 2:
Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0Example 3:
Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1Note:
1 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] = 1
1 <= initial.length < graph.length
0 <= initial[i] < graph.length
解题思路:本题可以采用并查集。首先利用并查集把graph中各个节点进行分组,同一个组的元素只要有一个被infected,那么其他的都会被infected,接下来遍历initial中的元素,如果initial中的元素有两个或者两个以上属于同一个组的话,那么从initial中去除这几个元素中的任何一个都无法减少infected nodes的数量。所以题目就演变成了从initial中找出和其他元素均不属于同一个组,并且这个元素的所属的组包含最多的元素。最后要提醒下,题目要求返回如果有多个答案的话返回索引最小的那个,这里指的不是initial中的索引,而是graph中的索引,就是指数值最小的值。
吐槽:Leetcode UI改版后,感觉看题目和提交代码没这么方便了。
代码如下:
class Solution(object):
def minMalwareSpread(self, graph, initial):
"""
:type graph: List[List[int]]
:type initial: List[int]
:rtype: int
"""
parent = [i for i in range(len(graph))] def find(v,p):
if p[v] == v:
return v
return find(p[v],p) def union(v1,v2):
p1 = find(v1,parent)
p2 = find(v2,parent)
if p1 < p2:
parent[p2] = p1
else:
parent[p1] = p2 for i in range(len(graph)):
for j in range(len(graph[i])):
if i != j and graph[i][j] == 1:
union(i,j)
dic = {}
for i in range(len(graph)):
p = find(i, parent)
dic[p] = dic.setdefault(p,0) + 1 pl = []
for i in initial:
p = find(i,parent)
pl.append(p) res = None
count = 0
for i in initial:
p = find(i, parent)
if pl.count(p) == 1:
if count < dic[p]:
count = dic[p]
res = i
elif count == dic[p]:
res = min(res,i) if res == None:
res = min(initial) return res
【leetcode】924.Minimize Malware Spread的更多相关文章
- [LeetCode] 924. Minimize Malware Spread 最大程度上减少恶意软件的传播
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...
- [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【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 ...
随机推荐
- 如何使用WidsMob Montage—蒙太奇制作有趣的动物照片?
今天,越来越多的人有宠物.根据最近的一项调查,超过六成的美国人在家中至少有一只宠物.这些宠物不是动物,而是家庭成员.因此,有趣的动物照片成为社交媒体上的热门话题是有道理的.当您打开朋友圈或短视频APP ...
- ExtJS5.0 菜鸟的第一天
1.新项目开始啦,后台用户管理页面涉及到表格数据添加,修改内容比较多.准备用EXTJS框架搞下,对于我这种JS不咋地的人来说,还真是个挑战.整了2天,才搞出一个Hello,world!我也是醉了.. ...
- Halo(十二)
@RequestBody @ResponseBody @RequestBody 1) 该注解用于读取 Request 请求的 body 部分数据,使用系统默认配置的 HttpMessageConver ...
- 每天一个linux命令:more(13)
more more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻 ...
- git 配置(实用)
将公钥添加进入即可 码云是添加私钥(注意!!!!) 这个时候Git就配置好了 git clone ssh协议的仓库 就可以直接拉代码了
- 接触python的第1天:测试hello world
在python3.8的平台可以输入了hello world, ide还能当做计算器 >>> print("hello world") hello world &g ...
- 【MySQL】selectKey获取insert后的自动主键
<insert id="insert" parameterType="cc.mrbird.febs.energy.domain.ChatGroup"> ...
- Scribd每月共有超过两亿个访客、累积数亿篇以上的文件档案,Alexa全球排名200以内
目前已登上世界300大网站,每月共有超过两亿个访客.累积数亿篇以上的文件档案.透过Flash介面的阅读器-iPaper,使用者可以在网站内浏览各种文件,由于该网站是一个文件分享平台,所有的文件都是由使 ...
- Class-DbConnectionManipulator:Execute,QueryFirstDefault<>
ylbtech-Class-DbConnectionManipulator:Execute,QueryFirstDefault<> 1.返回顶部 1. public object GetS ...
- python导入自定义模块和包
参考资料 https://blog.csdn.net/gvfdbdf/article/details/52084144 http://www.runoob.com/python/python-modu ...