Skip to content

Commit

Permalink
Added codes for 24 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 24, 2024
1 parent d1c9a51 commit dd34100
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
42 changes: 42 additions & 0 deletions GeeksForGeeks/February/24-2-24/GFG.java
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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/24-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/February/24-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|meetings|*logn)
Space complexity - O(n)
90 changes: 90 additions & 0 deletions LeetCode/February/24-2-24/Solution.java
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;
}
}

0 comments on commit dd34100

Please sign in to comment.