diff --git a/GeeksForGeeks/6-1-24/GFG.java b/GeeksForGeeks/6-1-24/GFG.java new file mode 100644 index 0000000..6669483 --- /dev/null +++ b/GeeksForGeeks/6-1-24/GFG.java @@ -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; + } +} diff --git a/GeeksForGeeks/6-1-24/README.md b/GeeksForGeeks/6-1-24/README.md new file mode 100644 index 0000000..9f16081 --- /dev/null +++ b/GeeksForGeeks/6-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*sqrt(n)) +Space complexity - O(n*sqrt(n)) diff --git a/LeetCode/6-1-24/README.md b/LeetCode/6-1-24/README.md new file mode 100644 index 0000000..09eedf8 --- /dev/null +++ b/LeetCode/6-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(sort) +Space complexity - O(n) diff --git a/LeetCode/6-1-24/Solution.java b/LeetCode/6-1-24/Solution.java new file mode 100644 index 0000000..8ff84eb --- /dev/null +++ b/LeetCode/6-1-24/Solution.java @@ -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 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; + } +}