Skip to content

Commit 063d91c

Browse files
committed
🚀 01-Sep-2020
1 parent 6bdb62b commit 063d91c

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Given a non-empty array of unique positive integers A, consider the following graph:
2+
3+
There are A.length nodes, labelled A[0] to A[A.length - 1];
4+
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
5+
Return the size of the largest connected component in the graph.
6+
7+
8+
9+
Example 1:
10+
11+
Input: [4,6,15,35]
12+
Output: 4
13+
14+
Example 2:
15+
16+
Input: [20,50,9,63]
17+
Output: 2
18+
19+
Example 3:
20+
21+
Input: [2,3,6,7,4,12,21,39]
22+
Output: 8
23+
24+
Note:
25+
26+
1 <= A.length <= 20000
27+
1 <= A[i] <= 100000
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
int MAX=100001;
41+
42+
int find(int x, vector<int> &parent){
43+
if(parent[x]==-1) return x;
44+
parent[x]=find(parent[x], parent);
45+
return parent[x];
46+
}
47+
48+
void unionn(int x, int y, vector<int> &parent){
49+
int xp=find(x, parent);
50+
int yp=find(y, parent);
51+
52+
if(xp!=yp)
53+
parent[yp]=xp;
54+
}
55+
56+
int largestComponentSize(vector<int>& A) {
57+
int n=A.size();
58+
vector<int> parent(MAX, -1);
59+
60+
for(int i=0;i<n;i++){
61+
for(int j=2; j<=sqrt(A[i]);j++){
62+
if(A[i]%j==0){
63+
unionn(A[i], j, parent);
64+
unionn(A[i], A[i]/j, parent);
65+
}
66+
}
67+
}
68+
69+
unordered_map<int, int> mp;
70+
for(int i=0;i<n;i++){
71+
int xp=find(A[i], parent);
72+
mp[xp+1]++;
73+
}
74+
int res=0;
75+
for(auto it=mp.begin(); it!=mp.end(); it++){
76+
res=max(res, it->second);
77+
}
78+
return res;
79+
}
80+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Given an array of 4 digits, return the largest 24 hour time that can be made.
2+
3+
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
4+
5+
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
6+
7+
8+
9+
Example 1:
10+
11+
Input: [1,2,3,4]
12+
Output: "23:41"
13+
Example 2:
14+
15+
Input: [5,5,5,5]
16+
Output: ""
17+
18+
19+
Note:
20+
21+
A.length == 4
22+
0 <= A[i] <= 9
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
class Solution {
35+
public:
36+
string largestTimeFromDigits(vector<int>& A) {
37+
string res;
38+
sort(A.begin(), A.end());
39+
do{
40+
string hours = to_string(A[0])+to_string(A[1]);
41+
string minutes = to_string(A[2])+to_string(A[3]);
42+
if(hours < "24" && minutes < "60") res=hours+":"+minutes; // sorting helped here
43+
44+
}while(next_permutation(A.begin(), A.end()));
45+
return res;
46+
}
47+
};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
2+
3+
Now your job is to find the total Hamming distance between all pairs of the given numbers.
4+
5+
Example:
6+
Input: 4, 14, 2
7+
8+
Output: 6
9+
10+
Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
11+
showing the four bits relevant in this case). So the answer will be:
12+
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
13+
Note:
14+
Elements of the given array are in the range of 0 to 10^9
15+
Length of the array will not exceed 10^4.
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
// Brute Force O(n2)
31+
32+
class Solution {
33+
public:
34+
35+
int getHamming(int x, int y){
36+
int ham=0;
37+
int tmp=x^y;
38+
for(int i=0;i<32;i++){
39+
ham+=tmp & 1;
40+
tmp=tmp>>1;
41+
}
42+
return ham;
43+
}
44+
45+
int totalHammingDistance(vector<int>& nums) {
46+
int n=nums.size();
47+
int res=0;
48+
for(int i=0;i<n;i++){
49+
for(int j=i+1;j<n;j++){
50+
res+=getHamming(nums[i], nums[j]);
51+
}
52+
}
53+
return res;
54+
}
55+
};
56+
57+
58+
59+
60+
61+
62+
63+
// O(n)
64+
65+
class Solution {
66+
public:
67+
int totalHammingDistance(vector<int>& nums) {
68+
int res=0;
69+
int n=nums.size();
70+
71+
for(int i=0;i<32;i++){
72+
int cnt=0;
73+
for(int j=0;j<n;j++){
74+
if(nums[j] & (1 << i))
75+
cnt++;
76+
}
77+
res+=cnt * (n-cnt);
78+
}
79+
return res;
80+
}
81+
};
82+
83+
84+
/*
85+
The total Hamming distance is constructed bit by bit in this approach.
86+
87+
Let's take a series of number: a1, a2, a3,..., an
88+
89+
Just think about all the Least Significant Bit (LSB) of a(k) (1 ≤ k ≤ n).
90+
91+
How many Hamming distance will they bring to the total?
92+
93+
If a pair of number has same LSB, the total distance will get 0.
94+
95+
If a pair of number has different LSB, the total distance will get 1.
96+
97+
For all number a1, a2, a3,..., a(n), if there are p numbers have 0 as LSB (put in set M), and q numbers have 1 for LSB (put in set N).
98+
99+
There are 2 situations:
100+
101+
Situation 1. If the 2 number in a pair both comes from M (or N), the total will get 0.
102+
103+
Situation 2. If the 1 number in a pair comes from M, the other comes from N, the total will get 1.
104+
105+
Since Situation 1 will add NOTHING to the total, we only need to think about Situation 2
106+
107+
How many pairs are there in Situation 2?
108+
109+
We choose 1 number from M (p possibilities), and 1 number from N (q possibilities).
110+
111+
The total possibilities is p × q = pq, which means
112+
113+
The total Hamming distance will get pq from LSB.
114+
If we remove the LSB of all numbers (right logical shift), the same idea can be used again and again until all numbers becomes zero
115+
116+
117+
*/
118+
119+
120+
121+
122+
// Source: https://leetcode.com/problems/total-hamming-distance/discuss/96243/Share-my-O(n)-C%2B%2B-bitwise-solution-with-thinking-process-and-explanation
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Given a non-empty array of unique positive integers A, consider the following graph:
2+
3+
There are A.length nodes, labelled A[0] to A[A.length - 1];
4+
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
5+
Return the size of the largest connected component in the graph.
6+
7+
8+
9+
Example 1:
10+
11+
Input: [4,6,15,35]
12+
Output: 4
13+
14+
Example 2:
15+
16+
Input: [20,50,9,63]
17+
Output: 2
18+
19+
Example 3:
20+
21+
Input: [2,3,6,7,4,12,21,39]
22+
Output: 8
23+
24+
Note:
25+
26+
1 <= A.length <= 20000
27+
1 <= A[i] <= 100000
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
int MAX=100001;
41+
42+
int find(int x, vector<int> &parent){
43+
if(parent[x]==-1) return x;
44+
parent[x]=find(parent[x], parent);
45+
return parent[x];
46+
}
47+
48+
void unionn(int x, int y, vector<int> &parent){
49+
int xp=find(x, parent);
50+
int yp=find(y, parent);
51+
52+
if(xp!=yp)
53+
parent[yp]=xp;
54+
}
55+
56+
int largestComponentSize(vector<int>& A) {
57+
int n=A.size();
58+
vector<int> parent(MAX, -1);
59+
60+
for(int i=0;i<n;i++){
61+
for(int j=2; j<=sqrt(A[i]);j++){
62+
if(A[i]%j==0){
63+
unionn(A[i], j, parent);
64+
unionn(A[i], A[i]/j, parent);
65+
}
66+
}
67+
}
68+
69+
unordered_map<int, int> mp;
70+
for(int i=0;i<n;i++){
71+
int xp=find(A[i], parent);
72+
mp[xp+1]++;
73+
}
74+
int res=0;
75+
for(auto it=mp.begin(); it!=mp.end(); it++){
76+
res=max(res, it->second);
77+
}
78+
return res;
79+
}
80+
};

0 commit comments

Comments
 (0)