diff --git a/GeeksForGeeks/February/18-2-24/GFG.java b/GeeksForGeeks/February/18-2-24/GFG.java new file mode 100644 index 0000000..d887c4d --- /dev/null +++ b/GeeksForGeeks/February/18-2-24/GFG.java @@ -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; + } +} diff --git a/GeeksForGeeks/February/18-2-24/README.md b/GeeksForGeeks/February/18-2-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/GeeksForGeeks/February/18-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/February/18-2-24/README.md b/LeetCode/February/18-2-24/README.md new file mode 100644 index 0000000..fbf4193 --- /dev/null +++ b/LeetCode/February/18-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * logn) +Space complexity - O(n) diff --git a/LeetCode/February/18-2-24/Solution.java b/LeetCode/February/18-2-24/Solution.java new file mode 100644 index 0000000..3bfa1b0 --- /dev/null +++ b/LeetCode/February/18-2-24/Solution.java @@ -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 occupied = new PriorityQueue<>((a, b) -> a.endTime == b.endTime ? + Integer.compare(a.roomId, b.roomId) + : Long.compare(a.endTime, b.endTime)); + Queue 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; + } +}