Skip to content

Commit

Permalink
Added codes for 18 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 18, 2024
1 parent ae08e07 commit 94ed854
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
93 changes: 93 additions & 0 deletions GeeksForGeeks/February/18-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//{ Driver Code Starts
import java.util.*;

class Node
{
int data;
Node left, right;
Node(int key)
{
data = key;
left = right = null;
}
}

class leaf_nodes
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while(t-- > 0)
{
int n = sc.nextInt();
Node root = null;
for(int i = 0; i < n; i++)
{
int data = sc.nextInt();
root = insert(root, data);
}
Solution gfg = new Solution();
System.out.println(gfg.sumOfLeafNodes(root));
}
}

public static Node insert(Node root, int x)
{

if(root == null)
{
return (new Node(x));
}

if(x < root.data)
{
root.left = insert(root.left, x);
}
else if(x >= root.data)
{
root.right = insert(root.right, x);
}

return root;
}

}

// } Driver Code Ends


/*node class of the binary ssearch tree
class Node
{
int data;
Node left, right;
Node(int key)
{
data = key;
left = right = null;
}
}*/
class Solution
{
static int sum;
public static int sumOfLeafNodes(Node root)
{
// code here
sum=0;
return sum(root);
}

private static int sum(Node root)
{
if (root == null)
return sum;

if (root.left == null && root.right == null)
sum += root.data;

sum(root.left);
sum(root.right);
return sum;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/18-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(1)
2 changes: 2 additions & 0 deletions LeetCode/February/18-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(n)
57 changes: 57 additions & 0 deletions LeetCode/February/18-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class T
{
public long endTime;
public int roomId;
public T(long endTime, int roomId)
{
this.endTime = endTime;
this.roomId = roomId;
}
}

class Solution
{
public int mostBooked(int n, int[][] meetings)
{
int[] count = new int[n];

Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

Queue<T> occupied = new PriorityQueue<>((a, b) -> a.endTime == b.endTime ?
Integer.compare(a.roomId, b.roomId)
: Long.compare(a.endTime, b.endTime));
Queue<Integer> availableRoomIds = new PriorityQueue<>();

for (int i = 0; i < n; ++i)
availableRoomIds.offer(i);

for (int[] meeting : meetings)
{
int start = meeting[0];
int end = meeting[1];

while (!occupied.isEmpty() && occupied.peek().endTime <= start)
availableRoomIds.offer(occupied.poll().roomId);

if (availableRoomIds.isEmpty())
{
T t = occupied.poll();
++count[t.roomId];
occupied.offer(new T(t.endTime + (end - start), t.roomId));
}
else
{
int roomId = availableRoomIds.poll();
++count[roomId];
occupied.offer(new T(end, roomId));
}
}

int maxIndex = 0;
for (int i = 0; i < n; ++i)
if (count[i] > count[maxIndex])
maxIndex = i;

return maxIndex;
}
}

0 comments on commit 94ed854

Please sign in to comment.