Skip to content

Commit fbdf39f

Browse files
committed
1101 added.
1 parent 98dac84 commit fbdf39f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/// Source : https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-06-30
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Sorting + Union Find
12+
/// Time Complexity: O(nlogn + n * a(n))
13+
/// Space Complexity: O(n)
14+
class UF{
15+
16+
private:
17+
vector<int> parent;
18+
int size;
19+
20+
public:
21+
UF(int n){
22+
for(int i = 0 ; i < n ; i ++)
23+
parent.push_back(i);
24+
size = n;
25+
}
26+
27+
int find(int p){
28+
if( p != parent[p] )
29+
parent[p] = find( parent[p] );
30+
return parent[p];
31+
}
32+
33+
bool isConnected(int p , int q){
34+
return find(p) == find(q);
35+
}
36+
37+
void unionElements(int p, int q){
38+
39+
int pRoot = find(p);
40+
int qRoot = find(q);
41+
42+
if( pRoot == qRoot )
43+
return;
44+
45+
parent[pRoot] = qRoot;
46+
size --;
47+
}
48+
49+
int sz(){
50+
return size;
51+
}
52+
};
53+
54+
class Solution {
55+
public:
56+
int earliestAcq(vector<vector<int>>& logs, int N) {
57+
58+
sort(logs.begin(), logs.end(),
59+
[](const vector<int>& log1, const vector<int>& log2){
60+
return log1[0] < log2[0];
61+
});
62+
63+
UF uf(N);
64+
for(const vector<int>& log: logs){
65+
uf.unionElements(log[1], log[2]);
66+
if(uf.sz() == 1) return log[0];
67+
}
68+
return -1;
69+
}
70+
};
71+
72+
73+
int main() {
74+
75+
return 0;
76+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
750750
| | | | | | |
751751
| 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [] | [C++](1099-Two-Sum-Less-Than-K/cpp-1099/) | | |
752752
| 1100 | [Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/) | [] | [C++](1100-Find-K-Length-Substrings-With-No-Repeated-Characters/cpp-1100/) | | |
753+
| 1101 | [The Earliest Moment When Everyone Become Friends](https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/) | [] | [C++](1101-The-Earliest-Moment-When-Everyone-Become-Friends/cpp-1101/) | | |
753754
| | | | | | |
754755
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/) | [solution](https://leetcode.com/problems/distribute-candies-to-people/solution/) | [C++](1103-Distribute-Candies-to-People/cpp-1103/) | | |
755756
| 1104 | [Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/) | [] | [C++](1104-Path-In-Zigzag-Labelled-Binary-Tree/cpp-1104/) | | |

0 commit comments

Comments
 (0)