-
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
ae08e07
commit 94ed854
Showing
4 changed files
with
154 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,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; | ||
} | ||
} |
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(1) |
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 * 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,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; | ||
} | ||
} |