Skip to content

Commit

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

class Node {
int data;
Node left, right;

Node(int val) {
data = val;
left = right = null;
}
}

public class GFG
{
public static void printList(Node head) {
while (head != null) {
if (head.left != null)
System.out.print("-1 ");
System.out.print(head.data + " ");
head = head.right;
}
System.out.println();
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine(); // Consume newline
while (t-- > 0) {
String str = sc.nextLine();
Node root = buildTree(str);
Solution ob = new Solution();
Node ans = ob.flattenBST(root);
printList(ans);
}
sc.close();
}

private static Node buildTree(String str) {
if (str.length() == 0 || str.charAt(0) == 'N')
return null;

String[] ip = str.split("\\s+");
int index = 0;

Node root = new Node(Integer.parseInt(ip[index++]));
Queue<Node> queue = new LinkedList<>();
queue.add(root);

while (!queue.isEmpty() && index < ip.length) {
Node currNode = queue.poll();

String currVal = ip[index++];
if (!currVal.equals("N")) {
currNode.left = new Node(Integer.parseInt(currVal));
queue.add(currNode.left);
}

if (index >= ip.length)
break;
currVal = ip[index++];
if (!currVal.equals("N")) {
currNode.right = new Node(Integer.parseInt(currVal));
queue.add(currNode.right);
}
}

return root;
}
}

// } Driver Code Ends


//User function Template for Java
class Solution
{
static Node prev;

public Node flattenBST(Node root)
{
// Code here
Node dummy = new Node(-1);
prev = dummy;

Inorder(root);

prev.left = null;
prev.right = null;
Node ret = dummy.right;

return ret;
}

private void Inorder(Node curr)
{
if (curr == null)
return;

Inorder(curr.left);
prev.left = null;
prev.right = curr;
prev = curr;
Inorder(curr.right);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/16-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/16-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)
17 changes: 17 additions & 0 deletions LeetCode/February/16-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution
{
public int findLeastNumOfUniqueInts(int[] arr, int k)
{
Map<Integer, Integer> count = new HashMap<>();

for (int a : arr)
count.merge(a, 1, Integer::sum);

Queue<Integer> minHeap = new PriorityQueue<>(count.values());

while (k > 0)
k -= minHeap.poll();

return minHeap.size() + (k < 0 ? 1 : 0);
}
}

0 comments on commit 56f77e7

Please sign in to comment.