diff --git a/GeeksForGeeks/February/16-2-24/GFG.java b/GeeksForGeeks/February/16-2-24/GFG.java new file mode 100644 index 0000000..52de2a9 --- /dev/null +++ b/GeeksForGeeks/February/16-2-24/GFG.java @@ -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 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); + } +} diff --git a/GeeksForGeeks/February/16-2-24/README.md b/GeeksForGeeks/February/16-2-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/February/16-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/February/16-2-24/README.md b/LeetCode/February/16-2-24/README.md new file mode 100644 index 0000000..905c94b --- /dev/null +++ b/LeetCode/February/16-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*logn) +Space complexity - O(n) diff --git a/LeetCode/February/16-2-24/Solution.java b/LeetCode/February/16-2-24/Solution.java new file mode 100644 index 0000000..ad18909 --- /dev/null +++ b/LeetCode/February/16-2-24/Solution.java @@ -0,0 +1,17 @@ +class Solution +{ + public int findLeastNumOfUniqueInts(int[] arr, int k) + { + Map count = new HashMap<>(); + + for (int a : arr) + count.merge(a, 1, Integer::sum); + + Queue minHeap = new PriorityQueue<>(count.values()); + + while (k > 0) + k -= minHeap.poll(); + + return minHeap.size() + (k < 0 ? 1 : 0); + } +}