-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1c9a51
commit dd34100
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
class GfG | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-->0) | ||
{ | ||
int n = sc.nextInt(); | ||
Solution ob = new Solution(); | ||
System.out.println(ob.maxSum(n)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public int fn(int n) | ||
{ | ||
int a=n/2, b=n/3, c=n/4; | ||
|
||
if(a+b+c>n) | ||
return fn(a)+fn(b)+fn(c); | ||
|
||
else | ||
return n; | ||
} | ||
|
||
public int maxSum(int n) | ||
{ | ||
return fn(n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(|meetings|*logn) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
class UnionFind | ||
{ | ||
public UnionFind(int n) | ||
{ | ||
id = new int[n]; | ||
rank = new int[n]; | ||
for (int i = 0; i < n; ++i) | ||
id[i] = i; | ||
} | ||
|
||
public void unionByRank(int u, int v) | ||
{ | ||
int i = find(u); | ||
int j = find(v); | ||
if (i == j) | ||
return; | ||
if (rank[i] < rank[j]) | ||
{ | ||
id[i] = j; | ||
} | ||
else if (rank[i] > rank[j]) | ||
{ | ||
id[j] = i; | ||
} | ||
else | ||
{ | ||
id[i] = j; | ||
++rank[j]; | ||
} | ||
} | ||
|
||
public boolean connected(int u, int v) | ||
{ | ||
return find(u) == find(v); | ||
} | ||
|
||
public void reset(int u) | ||
{ | ||
id[u] = u; | ||
} | ||
|
||
private int[] id; | ||
private int[] rank; | ||
|
||
private int find(int u) | ||
{ | ||
return id[u] == u ? u : (id[u] = find(id[u])); | ||
} | ||
} | ||
|
||
class Solution | ||
{ | ||
public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) | ||
{ | ||
List<Integer> ans = new ArrayList<>(); | ||
UnionFind uf = new UnionFind(n); | ||
TreeMap<Integer, List<Pair<Integer, Integer>>> timeToPairs = new TreeMap<>(); | ||
|
||
uf.unionByRank(0, firstPerson); | ||
|
||
for (int[] m : meetings) | ||
{ | ||
timeToPairs.putIfAbsent(m[2], new ArrayList<>()); | ||
timeToPairs.get(m[2]).add(new Pair<>(m[0], m[1])); | ||
} | ||
|
||
for (List<Pair<Integer, Integer>> pairs : timeToPairs.values()) | ||
{ | ||
Set<Integer> peopleUnioned = new HashSet<>(); | ||
for (Pair<Integer, Integer> pair : pairs) | ||
{ | ||
int x = pair.getKey(); | ||
int y = pair.getValue(); | ||
uf.unionByRank(x, y); | ||
peopleUnioned.add(x); | ||
peopleUnioned.add(y); | ||
} | ||
|
||
for (final int person : peopleUnioned) | ||
if (!uf.connected(person, 0)) | ||
uf.reset(person); | ||
} | ||
|
||
for (int i = 0; i < n; ++i) | ||
if (uf.connected(i, 0)) | ||
ans.add(i); | ||
|
||
return ans; | ||
} | ||
} |