From 4134dfdb6db8185009b3f1314a40c5bca0cfb513 Mon Sep 17 00:00:00 2001 From: Ankita Date: Sun, 23 Oct 2022 12:23:18 +0530 Subject: [PATCH 1/2] adding existing files --- Sorting/mergeSort.java | 148 ++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 67 deletions(-) diff --git a/Sorting/mergeSort.java b/Sorting/mergeSort.java index e0129a5..297475f 100644 --- a/Sorting/mergeSort.java +++ b/Sorting/mergeSort.java @@ -1,72 +1,86 @@ +class Sort +{ + void merge(int arr[], int left, int middle, int right) + { + int low = middle - left + 1; + int high = right - middle; + + int L[] = new int[low]; + int R[] = new int[high]; -public class MergeSort { - - public static void main(String args[]) { - int arr[] = {23, 12, 85, 39, 98, 7, 16, 97, 54}; - - System.out.println("Given Array"); - printArray(arr); - - MergeSort ob = new MergeSort(); - ob.sort(arr, 0, arr.length - 1); - - System.out.println("\nSorted array"); - printArray(arr); - } - - void merge(int arr[], int low, int mid, int up) { - int n1 = mid - low + 1; - int n2 = up - mid; - - int Left[] = new int[n1]; - int Right[] = new int[n2]; - - for (int i = 0; i < n1; ++i) - Left[i] = arr[low + i]; - for (int j = 0; j < n2; ++j) - Right[j] = arr[mid + 1 + j]; - - int i = 0, j = 0; - - int k = low; - while (i < n1 && j < n2) { - if (Left[i] <= Right[j]) { - arr[k] = Left[i]; - i++; - } - else { - arr[k] = Right[j]; - j++; - } - k++; - } - while (i < n1) { - arr[k] = Left[i]; - i++; - k++; - } - while (j < n2) { - arr[k] = Right[j]; - j++; - k++; - } - } + int i = 0, j = 0; + + for (i = 0; i < low; i++) + { + L[i] = arr[left + i]; + } + for (j = 0; j < high; j++) + { + R[j] = arr[middle + 1 + j]; + } + + + int k = left; + i = 0; + j = 0; - void sort(int arr[], int low, int up) { - if (low < up) { - int mid =low + (up-low)/2; + while (i < low && j < high) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < low) + { + arr[k] = L[i]; + i++; + k++; + } + + while (j < high) + { + arr[k] = R[j]; + j++; + k++; + } + } + - sort(arr, low, mid); - sort(arr, mid + 1, up); + void mergeSort(int arr[], int left, int right) + { + int middle; + if (left < right) { + middle = (left + right) / 2; + + mergeSort(arr, left, middle); + mergeSort(arr, middle + 1, right); + + merge(arr, left, middle, right); + } + } + + void display(int arr[]) + { + for (int i=0; i Date: Sun, 23 Oct 2022 12:28:16 +0530 Subject: [PATCH 2/2] added a job sequencing problem --- jobSequencingProbem.cpp | 71 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 jobSequencingProbem.cpp diff --git a/jobSequencingProbem.cpp b/jobSequencingProbem.cpp new file mode 100644 index 0000000..862b432 --- /dev/null +++ b/jobSequencingProbem.cpp @@ -0,0 +1,71 @@ +// You are given a set of N jobs where each job comes with a deadline and profit. +// The profit can only be earned upon completing the job within its deadline. +// Find the number of jobs done and the maximum profit that can be obtained. +// Each job takes a single unit of time and only one job can be performed at a time. + +// Basic Outline of the approach:- + +// Sort the jobs in descending order of profit. +// If the maximum deadline is x, make an array of size x .Each array index is set to -1 initially as no jobs have been performed yet. +// For every job check if it can be performed on its last day. +// If possible mark that index with the job id and add the profit to our answer. +// If not possible, loop through the previous indexes until an empty slot is found. + +// Time Complexity: O(N log N) + O(N*M). +// Space Complexity: O(M) + +#include + +using namespace std; +// A structure to represent a job +struct Job { + int id; // Job Id + int dead; // Deadline of job + int profit; // Profit if job is over before or on deadline +}; +class Solution { + public: + bool static comparison(Job a, Job b) { + return (a.profit > b.profit); + } + //Function to find the maximum profit and the number of jobs done + pair < int, int > JobScheduling(Job arr[], int n) { + + sort(arr, arr + n, comparison); + int maxi = arr[0].dead; + for (int i = 1; i < n; i++) { + maxi = max(maxi, arr[i].dead); + } + + int slot[maxi + 1]; + + for (int i = 0; i <= maxi; i++) + slot[i] = -1; + + int countJobs = 0, jobProfit = 0; + + for (int i = 0; i < n; i++) { + for (int j = arr[i].dead; j > 0; j--) { + if (slot[j] == -1) { + slot[j] = i; + countJobs++; + jobProfit += arr[i].profit; + break; + } + } + } + + return make_pair(countJobs, jobProfit); + } +}; +int main() { + int n = 4; + Job arr[n] = {{1,4,20},{2,1,10},{3,2,40},{4,2,30}}; + + Solution ob; + //function call + pair < int, int > ans = ob.JobScheduling(arr, n); + cout << ans.first << " " << ans.second << endl; + + return 0; +} \ No newline at end of file