From d8c35ba288fbbf6006cf281f10d00d6f67d1c954 Mon Sep 17 00:00:00 2001 From: KSM Date: Mon, 24 Oct 2022 10:26:04 +0530 Subject: [PATCH 1/2] Add least-number-of-unique-integers-after-k-removals --- ...r-of-unique-integers-after-k-removals.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Hacktoberfest2022/least-number-of-unique-integers-after-k-removals.java diff --git a/Hacktoberfest2022/least-number-of-unique-integers-after-k-removals.java b/Hacktoberfest2022/least-number-of-unique-integers-after-k-removals.java new file mode 100644 index 0000000..132dba8 --- /dev/null +++ b/Hacktoberfest2022/least-number-of-unique-integers-after-k-removals.java @@ -0,0 +1,38 @@ +/* +Author: Kabir Singh Mehrok + +Title: Least Number of Unique Integers after K Removals +Description: Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements. +*/ + +class Solution { + public int findLeastNumOfUniqueInts(int[] arr, int k) { + int N = arr.length; + HashMap h = new HashMap(); + for (int i=0; i= k + + ArrayList a = new ArrayList(h.values()); + Collections.sort(a); + + int sum = 0; + int i = 0; + for (i=0; i= k) break; + sum += a.get(i); + } + + // now there can be two cases + // if sum > k, it implies that we have taken an extra element into account + // if sum == k, it implies we have exact amount of element + + if (sum > k) return a.size() - i + 1; + if (sum == k) return a.size() - i; + else return 0; + } +} \ No newline at end of file From 6b62f86cf306a93efebff9f40aab195d22e7f186 Mon Sep 17 00:00:00 2001 From: KSM Date: Mon, 24 Oct 2022 10:29:41 +0530 Subject: [PATCH 2/2] Add count-good-nodes-in-binary-tree --- .../count-good-nodes-in-binary-tree.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Hacktoberfest2022/count-good-nodes-in-binary-tree.java diff --git a/Hacktoberfest2022/count-good-nodes-in-binary-tree.java b/Hacktoberfest2022/count-good-nodes-in-binary-tree.java new file mode 100644 index 0000000..179c0d0 --- /dev/null +++ b/Hacktoberfest2022/count-good-nodes-in-binary-tree.java @@ -0,0 +1,39 @@ +/* +Author: Kabir Singh Mehrok + +Title: Count Good Nodes in Binary Tree +Description: Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. +*/ + + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode() {} + TreeNode(int val) { this.val = val; } + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public int goodNodes(TreeNode root) { + // dfs traversal to every node + // keep track of the max val along the path + // if given val > maxval, +1 and recurse, with updated maxval + // else, traverse + return recurse(root, root.val); + } + + public int recurse(TreeNode root, int maxval) + { + if (root == null) return 0; + if (root.val >= maxval) { + return 1 + recurse(root.right, root.val) + recurse(root.left, root.val); + } + else return recurse(root.right, maxval) + recurse(root.left, maxval); + } +} \ No newline at end of file