题目如下:

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. boost function bind ref

    boost::function to encapsulate function pointers. 1. function #include <boost/function.hpp> #i ...

  2. 4412 搭建tftp服务器

    搭建服务器 --安装xinetd,sudo apt-get install xinetd --安装tftp和tftpd,sudo apt-get install tftp tftpd --配置/etc ...

  3. 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】

    题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...

  4. 【Linux】shell脚本参数传递

    这里介绍参数传递的两种方式. 方式一:$0,$1,$2... 采用$0,$1,$2..等方式获取脚本命令行传入的参数 $0:脚本名称 $1....: 参数 例子: #编写一个shell $ vim t ...

  5. loadrunner(预测系统行为和性能的负载测试工具)

    LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用LoadRu ...

  6. [CSP-S模拟测试]:v(hash表+期望DP)

    题目背景 $\frac{1}{4}$遇到了一道水题,又完全不会做,于是去请教小$D$.小$D$看了$0.607$眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{ ...

  7. tomcat启动前端项目

    前后端分离项目,前端使用vue,部署启动前端项目可以使用NodeJS,Nginx,Tomcat. *)使用Tomcat部署启动: 1.把vue项目build生成的dist包,放到Tomcat的weba ...

  8. 热经-北京中地时空数码科技有限公司-研发工程师(WEBGIS方向)

    一面: 登记,填写个人信息 笔试 选择题: HTML,CSS,JS 的选择题,都是基础题.其中有一道问哪个不是 document 的属性或方法,我在 bgColor 和 focus() 上面纠结了一下 ...

  9. css缓存问题

    频繁更换样式,会导致样式缓存, 在实际项目开发过过程中,页面是上传到服务器上的.而为了减少服务器的压力,让用户少加载,浏览器会将图片.css.js缓存到本地中,以便下次访问网站时使用.这样做不仅减少了 ...

  10. 每天一个linux常用命令--ls 和 -ll 有什么区别?

    一.-ls 和 -ll  有什么区别? 1. ls 命令可以说是linux下最常用的命令之一.ll不是命令,是ls -l的别名相当于windows里的快捷方式.所以"ll"和“ls ...