题目如下:

In a network of nodes, each node i is directly connected to another node j if and only if graph[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 minimize M(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: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 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的更多相关文章

  1. [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 ...

  2. [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二

    (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...

  3. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  4. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  5. [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II

    (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Docker安装RMQ

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11752934.html 进入rabbitmq的docker hub镜像仓库地址:https://hub ...

  2. 【leetcode】436. Find Right Interval

    题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...

  3. TypeError:NoneType object is not callable情况下的错误

    只用在该视图函数下面加上return ‘值’

  4. SimpleDateFormat线程不安全原因及解决方案

    一. 线程不安全验证: /** * SimpleDateFormat线程安全测试 * 〈功能详细描述〉 * * @author 17090889 * @see [相关类/方法](可选) * @sinc ...

  5. Angular JS - 8 - SeaJS与前端模块化

    一.前端模块化 关于前端模块化,参考以下链接 : https://github.com/seajs/seajs/issues/547 http://www.cnblogs.com/huiguo/cat ...

  6. 集训队8月9日(组合计数+容斥原理+Mobius函数)

    刷题数:4 今天看了组合计数+容斥原理+Mobius函数,算法竞赛进阶指南169~179页 组合计数 https://www.cnblogs.com/2462478392Lee/p/11328938. ...

  7. LintCode之奇偶分割数组

    题目描述: 我的分析:题目要求将奇数放在偶数的前面,没有要求将奇数或偶数排序,因此我可以设置两个指针,一个(i)指向数组第一个数字,另一个(j)指向数组的最后一个数字,因为奇数要放在前面,所以从后往前 ...

  8. ORACLE基本用法及常用命令

    切换ORACLE用户 su - oracle ---------------------------- 重启数据库 sqlplus sys / as sysdba shutdown immediate ...

  9. js另存为、打印、属性、加入收藏、关闭等代码

    js打开代码 <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开> &l ...

  10. CET-6 分频周计划生词筛选(Week 2)

    点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...