【leetcode】924.Minimize Malware Spread
题目如下:
In a network of nodes, each node
iis directly connected to another nodejif and only ifgraph[i][j] = 1.Some nodes
initialare 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
initiallist 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 <= 3000 <= graph[i][j] == graph[j][i] <= 1graph[i][i] = 11 <= initial.length < graph.length0 <= 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 ...
随机推荐
- 使用Fiddler为满足某些特定格式的网络请求返回mock响应
假设我想对本地Java程序发起的调用SAP Hybris web service https://jerrywang.com:9002/rest/v2/electronics/users/ 这个网络请 ...
- 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal
题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...
- wnmp的配置
第一部分:准备工作.(系统:Windows 8.1) 1.首先是下载软件. NGINX-1.3.8官网下载:http://nginx.org/en/download.html PHP5.4.8版本下载 ...
- 【Vue】axios post提交请求转为form data
axios.js import axios from 'axios'; import qs from 'qs'; // axios 配置 axios.defaults.timeout = 8000; ...
- bugku | login2(SKCTF) 200
在响应包里面发现tips,base64解码后看到提示信息: $sql="SELECT username,password FROM admin WHERE username='". ...
- 23-25 October in 614
Practice sort 给定一系列形如 \(A<B\) 的不等关系,判断前 \(k\) 个不等关系是否即可确定 \(n\) 个元素之间的大小顺序:如果不可确定,判断前 \(k\) 个不等关系 ...
- [CSP-S模拟测试]:蔬菜(二维莫队)
题目描述 小$C$在家中开垦了一块菜地,可以抽象成一个$r\times c$大小的矩形区域,菜地的每个位置都种着一种蔬菜.秋天到了,小$C$家的菜地丰收了. 小$C$拟定了$q$种采摘蔬菜的计划,计划 ...
- 经典JS 判断上传文件大小和JS即时同步电脑时间
我也是新手,还是一个JS笨,有一些网站要实现的功能要自己写么? 答案是不会,去问同事大佬吧,闲简单.就在晚上看了一些其他大佬们写的JS效果, 代码很少.占用网站CPU也小的多.可以一用, 废话少扯.代 ...
- Oracle下定时删除归档日志脚本
一.报错信息 前几天网站突然访问不了,并且报了如下错误: ora-27101: shared memory realm does not exist ora-01034: oracle not ava ...
- Thrift报错:Error: Thrift compiler: Failed to translate files. Error: Cannot run program thrift error=2
文章目录 报错: 原因: 解决: 报错: Error: Thrift compiler: Failed to translate files. Error: Cannot run program th ...