-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
927dec7
commit c6ef458
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(sort) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |