Skip to content

Commit

Permalink
Added for 6 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 6, 2024
1 parent 927dec7 commit c6ef458
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GeeksForGeeks/6-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while(t-- > 0){

long a;
a = Long.parseLong(br.readLine().trim());


long b;
b = Long.parseLong(br.readLine().trim());

Solution obj = new Solution();
long res = obj.sumOfPowers(a, b);

System.out.println(res);

}
}
}

// } Driver Code Ends


class Solution
{
public static long sumOfPowers(long a, long b)
{
// code here
long sum = 0;
for(long i=a; i<=b; i++)
{
sum += pf(i);
}
return sum;
}


public static long pf(long n)
{
long count =0;
while (n%2==0)
{
count++;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
while (n%i == 0)
{
count++;
n /= i;
}
}
if (n > 2)
count++;

return count;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/6-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*sqrt(n))
Space complexity - O(n*sqrt(n))
2 changes: 2 additions & 0 deletions LeetCode/6-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(sort)
Space complexity - O(n)
45 changes: 45 additions & 0 deletions LeetCode/6-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Job {
public int startTime;
public int endTime;
public int profit;
public Job(int startTime, int endTime, int profit) {
this.startTime = startTime;
this.endTime = endTime;
this.profit = profit;
}
}

class Solution {
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
final int n = startTime.length;

Job[] jobs = new Job[n];

for (int i = 0; i < n; ++i)
jobs[i] = new Job(startTime[i], endTime[i], profit[i]);

Arrays.sort(jobs, (a, b) -> a.startTime - b.startTime);

// Will use binary search to find the first available startTime
for (int i = 0; i < n; ++i)
startTime[i] = jobs[i].startTime;

return getMaxProfit(jobs);
}

private int getMaxProfit(Job[] jobs) {
int maxProfit = 0;
Queue<Job> minHeap = new PriorityQueue<>((a, b) -> a.endTime - b.endTime);

for (Job job : jobs) {
while (!minHeap.isEmpty() && job.startTime >= minHeap.peek().endTime)
maxProfit = Math.max(maxProfit, minHeap.poll().profit);
minHeap.offer(new Job(job.startTime, job.endTime, job.profit + maxProfit));
}

while (!minHeap.isEmpty())
maxProfit = Math.max(maxProfit, minHeap.poll().profit);

return maxProfit;
}
}

0 comments on commit c6ef458

Please sign in to comment.