From 425fd04040d34ec172e05d0f90686b692078a0a9 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Mon, 19 Feb 2024 07:59:14 +0530 Subject: [PATCH] Added codes for 19 Feb --- GeeksForGeeks/February/19-2-24/GFG.java | 60 ++++++++++++++++++++++++ GeeksForGeeks/February/19-2-24/README.md | 2 + LeetCode/February/19-2-24/README.md | 2 + LeetCode/February/19-2-24/Solution.java | 7 +++ 4 files changed, 71 insertions(+) create mode 100644 GeeksForGeeks/February/19-2-24/GFG.java create mode 100644 GeeksForGeeks/February/19-2-24/README.md create mode 100644 LeetCode/February/19-2-24/README.md create mode 100644 LeetCode/February/19-2-24/Solution.java diff --git a/GeeksForGeeks/February/19-2-24/GFG.java b/GeeksForGeeks/February/19-2-24/GFG.java new file mode 100644 index 0000000..bb2762c --- /dev/null +++ b/GeeksForGeeks/February/19-2-24/GFG.java @@ -0,0 +1,60 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.util.*; + +class GFG{ + public static void main(String args[])throws IOException + { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(in.readLine()); + while(t-- > 0){ + String s = in.readLine(); + int k = Integer.parseInt(in.readLine()); + + Solution ob = new Solution(); + System.out.println(ob.minValue(s, k)); + } + } +} +// } Driver Code Ends + + +// User function Template for Java + +class Solution +{ + static int minValue(String s, int k) + { + // code here + HashMap hm = new HashMap<>(); + + for(char c:s.toCharArray()) + hm.put(c,hm.getOrDefault(c,0)+1); + + PriorityQueue pq = new PriorityQueue<>((a,b)->b-a); + + for(Map.Entry e:hm.entrySet()) + pq.add(e.getValue()); + + while(k!=0) + { + int freq = pq.remove(); + freq--; + + if(freq>0) + pq.add(freq); + + k--; + } + + int ans = 0; + while(!pq.isEmpty()) + { + int freq = pq.remove(); + ans = ans + (int)Math.pow(freq,2); + } + return ans; + } +} diff --git a/GeeksForGeeks/February/19-2-24/README.md b/GeeksForGeeks/February/19-2-24/README.md new file mode 100644 index 0000000..e18a468 --- /dev/null +++ b/GeeksForGeeks/February/19-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * k * logp) +Space complexity - O(n) diff --git a/LeetCode/February/19-2-24/README.md b/LeetCode/February/19-2-24/README.md new file mode 100644 index 0000000..b85bed6 --- /dev/null +++ b/LeetCode/February/19-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(1) +Space complexity - O(1) diff --git a/LeetCode/February/19-2-24/Solution.java b/LeetCode/February/19-2-24/Solution.java new file mode 100644 index 0000000..54dd1e1 --- /dev/null +++ b/LeetCode/February/19-2-24/Solution.java @@ -0,0 +1,7 @@ +class Solution +{ + public boolean isPowerOfTwo(int n) + { + return n < 0 ? false : Integer.bitCount(n) == 1; + } +}