Skip to content

Commit

Permalink
Added codes for 11 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 11, 2024
1 parent 59de072 commit cdbbd4d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
47 changes: 47 additions & 0 deletions GeeksForGeeks/May/11-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//{ 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) {
long n = Long.parseLong(in.readLine());

Solution ob = new Solution();
List<Long> ans = new ArrayList<>();
StringBuilder out = new StringBuilder();
ans = ob.jugglerSequence(n);
for (int i = 0; i < ans.size(); i++) out.append(ans.get(i) + " ");
System.out.println(out);
}
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
static List<Long> jugglerSequence(long n)
{
// code here
List<Long> ans=new ArrayList<>();
ans.add(n);
while(n>1)
{
if(n%2==0)
n=(long)Math.pow(n,0.5);
else
n=(long)Math.pow(n,1.5);

ans.add(n);
}

return ans;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/11-5-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/May/11-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(sort + nlogk)
Space complexity - O(n)
32 changes: 32 additions & 0 deletions LeetCode/May/11-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution
{
public double mincostToHireWorkers(int[] quality, int[] wage, int k)
{
double ans = Double.MAX_VALUE;
int qualitySum = 0;
// (wagePerQuality, quality) sorted by wagePerQuality
Pair<Double, Integer>[] workers = new Pair[quality.length];
Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

for (int i = 0; i < quality.length; ++i)
workers[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);

Arrays.sort(workers, (a, b) -> Double.compare(a.getKey(), b.getKey()));

for (Pair<Double, Integer> worker : workers)
{
double wagePerQuality = worker.getKey();
int q = worker.getValue();
maxHeap.offer(q);
qualitySum += q;

if (maxHeap.size() > k)
qualitySum -= maxHeap.poll();

if (maxHeap.size() == k)
ans = Math.min(ans, qualitySum * wagePerQuality);
}

return ans;
}
}

0 comments on commit cdbbd4d

Please sign in to comment.