题目如下:

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:

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

Example 2:

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

Example 3:

  1. Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
  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改版后,感觉看题目和提交代码没这么方便了。

代码如下:

  1. class Solution(object):
  2. def minMalwareSpread(self, graph, initial):
  3. """
  4. :type graph: List[List[int]]
  5. :type initial: List[int]
  6. :rtype: int
  7. """
  8. parent = [i for i in range(len(graph))]
  9.  
  10. def find(v,p):
  11. if p[v] == v:
  12. return v
  13. return find(p[v],p)
  14.  
  15. def union(v1,v2):
  16. p1 = find(v1,parent)
  17. p2 = find(v2,parent)
  18. if p1 < p2:
  19. parent[p2] = p1
  20. else:
  21. parent[p1] = p2
  22.  
  23. for i in range(len(graph)):
  24. for j in range(len(graph[i])):
  25. if i != j and graph[i][j] == 1:
  26. union(i,j)
  27. dic = {}
  28. for i in range(len(graph)):
  29. p = find(i, parent)
  30. dic[p] = dic.setdefault(p,0) + 1
  31.  
  32. pl = []
  33. for i in initial:
  34. p = find(i,parent)
  35. pl.append(p)
  36.  
  37. res = None
  38. count = 0
  39. for i in initial:
  40. p = find(i, parent)
  41. if pl.count(p) == 1:
  42. if count < dic[p]:
  43. count = dic[p]
  44. res = i
  45. elif count == dic[p]:
  46. res = min(res,i)
  47.  
  48. if res == None:
  49. res = min(initial)
  50.  
  51. 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. 【leetcode】719. Find K-th Smallest Pair Distance

    题目如下: 解题思路:对于这一类知道上限和下限,求第N位是什么的题目,可以先看看二分查找的方法可不可行.首先对nums进行排序,很显然任意两个元素距离绝对值最小是0,最大是nums[-1] - num ...

  2. mui is not defined

    vue项目中引用mui.js,我是在main.js中这样引入的,    结果报错  查找资料,最后在mui.js的最后添加了这样一句  这是因为mui并不能像jquery那样作为全局对象存在,加上wi ...

  3. LDD3 第13章 USB驱动程序

    通用串行总线(USB)是主机和外围设备之间的一种连接.最新USB规范修订增加了理论上高达480Mbps的高速连接. 从拓扑上看,USB子系统并不是以总线的方式来布置的,它是一颗由几个点对点的连接构建而 ...

  4. 4412 make menuconfig和make

    一.Menuconfig的操作 • Linux编译器通过.config文件确认哪些代码编译进内核,哪些被裁减掉• menuconfig是生成.config的一个工具• 在Linux发展过程中,配置内核 ...

  5. [CSP-S模拟测试]:Read(数学)

    题目描述 热爱看书的你有$N$本书,第$i$本书的种类为$A[i]$.你希望每天能够看一本书,但是不希望连续两天看种类相同的书.为了达成这个条件,你需要选择一些书不看,作为一个好学生,你希望不看的书尽 ...

  6. HTML 3-列表、表单、语义化

    有序列表.无序列表.自定义列表在语义上的区别 1.有序列表ol>li:列表有顺序编号(默认列表前加1.,2.,3.,...编号),适用于有逻辑顺序的排列项目,而且次序不能乱.2.无序列表ul&g ...

  7. SpringBoot 快速构建微服务体系 知识点总结

    可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...

  8. jenkins clone代码时间太长怎么办?

    在Jenkins的默认配置中,clone代码时会拉取所有历史版本的代码,而且默认的超时时限只有10分钟.这就造成在某些项目中,由于代码量本身就比较大,历史版本也比较多,再加上网络环境不是特别好,Jen ...

  9. 0.tensorflow——常用说明

    1.tf tf.placeholder(tf.float32,shape=[None,inputSize])#类似于一个float32的声明 tf.reduce_mean()#求均值,可以是不同维度 ...

  10. Java集合的介绍

    参考博客: https://blog.csdn.net/zhangqunshuai/article/details/80660974 List , Set, Map都是接口,前两个继承至Collect ...