Skip to content

Commit aee1dbf

Browse files
committed
0924 added.
1 parent 5d5375f commit aee1dbf

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(D)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(D ${SOURCE_FILES})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// Source : https://leetcode.com/problems/minimize-malware-spread/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-13
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_set>
8+
9+
using namespace std;
10+
11+
12+
/// DFS
13+
/// Time Complexity: O(n^2)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
17+
private:
18+
int n;
19+
20+
public:
21+
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
22+
23+
unordered_set<int> init_set;
24+
for(int e: initial)
25+
init_set.insert(e);
26+
27+
n = graph.size();
28+
vector<bool> visited(n, false);
29+
30+
int best = 0, res = -1;
31+
for(int v = 0; v < n; v ++)
32+
if(init_set.count(v)){
33+
int cnt = dfs(graph, v, visited);
34+
if(cnt > best){
35+
best = cnt;
36+
res = v;
37+
}
38+
}
39+
return res;
40+
}
41+
42+
private:
43+
int dfs(const vector<vector<int>>& g, int v, vector<bool>& visited){
44+
45+
visited[v] = true;
46+
int res = 1;
47+
for(int next = 0; next < n; next ++)
48+
if(g[v][next] && !visited[next])
49+
res += dfs(g, next, visited);
50+
return res;
51+
}
52+
};
53+
54+
55+
int main() {
56+
57+
return 0;
58+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/// Source : https://leetcode.com/problems/minimize-malware-spread/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_set>
8+
9+
using namespace std;
10+
11+
12+
/// Using Union-Find
13+
/// Time Complexity: O(n^2)
14+
/// Space Complexity: O(n)
15+
class UF{
16+
17+
private:
18+
vector<int> parent, sz;
19+
20+
public:
21+
UF(int n){
22+
parent.clear();
23+
for(int i = 0 ; i < n ; i ++){
24+
parent.push_back(i);
25+
sz.push_back(1);
26+
}
27+
}
28+
29+
int find(int p){
30+
if( p != parent[p] )
31+
parent[p] = find( parent[p] );
32+
return parent[p];
33+
}
34+
35+
bool isConnected(int p , int q){
36+
return find(p) == find(q);
37+
}
38+
39+
void unionElements(int p, int q){
40+
41+
int pRoot = find(p);
42+
int qRoot = find(q);
43+
44+
if( pRoot == qRoot )
45+
return;
46+
47+
parent[pRoot] = qRoot;
48+
sz[qRoot] += sz[pRoot];
49+
}
50+
51+
int size(int p){
52+
return sz[find(p)];
53+
}
54+
};
55+
56+
class Solution {
57+
58+
public:
59+
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
60+
61+
int n = graph.size();
62+
UF uf(n);
63+
for(int i = 0; i < n; i ++)
64+
for(int j = i + 1; j < n; j ++)
65+
if(graph[i][j])
66+
uf.unionElements(i, j);
67+
68+
sort(initial.begin(), initial.end());
69+
int best = 0, res = -1;
70+
for(int v: initial)
71+
if(uf.size(v) > best){
72+
best = uf.size(v);
73+
res = v;
74+
}
75+
return res;
76+
}
77+
};
78+
79+
80+
int main() {
81+
82+
return 0;
83+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,4 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
568568
| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/) | [solution](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/solution/) | [C++](0921-Minimum-Add-to-Make-Parentheses-Valid/cpp-0921/) | | |
569569
| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/description/) | [solution](https://leetcode.com/problems/sort-array-by-parity-ii/solution/) | [C++](0922-Sort-Array-By-Parity-II/cpp-0922/) | | |
570570
| 923 | [3Sum With Multiplicity](https://leetcode.com/problems/3sum-with-multiplicity/description/) | [solution](https://leetcode.com/problems/3sum-with-multiplicity/solution/) | [C++](0923-3Sum-With-Multiplicity/cpp-0923/) | | |
571-
| | | | | | |
571+
| 924 | [Minimize Malware Spread](https://leetcode.com/problems/minimize-malware-spread/description/) | [solution](https://leetcode.com/problems/minimize-malware-spread/solution/) | [C++](0924-Minimize-Malware-Spread/cpp-0924/) | | |

0 commit comments

Comments
 (0)