Skip to content

Commit

Permalink
Added codes for 19 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 19, 2024
1 parent 94ed854 commit 425fd04
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
60 changes: 60 additions & 0 deletions GeeksForGeeks/February/19-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<Character,Integer> hm = new HashMap<>();

for(char c:s.toCharArray())
hm.put(c,hm.getOrDefault(c,0)+1);

PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);

for(Map.Entry<Character,Integer> 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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/19-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * k * logp)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/February/19-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(1)
Space complexity - O(1)
7 changes: 7 additions & 0 deletions LeetCode/February/19-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution
{
public boolean isPowerOfTwo(int n)
{
return n < 0 ? false : Integer.bitCount(n) == 1;
}
}

0 comments on commit 425fd04

Please sign in to comment.