Created
May 24, 2021 03:26
-
-
Save sauravgpt/32ca4cb7cd6520358b8aaef5f169aa83 to your computer and use it in GitHub Desktop.
Job Scheduling
This file contains hidden or 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
vector<int> JobScheduling(Job arr[], int n) { | |
vector<int> slots(n, -1); | |
sort(arr, arr+n, [&](const Job &a, const Job &b) -> bool { | |
return a.profit > b.profit; | |
}); | |
for(int i=0; i<n; i++) { | |
int index = min(arr[i].dead, n)-1; | |
while(index >= 0 && slots[index] != -1) index--; | |
if(index >=0) | |
slots[index] = arr[i].profit; | |
} | |
int totalProfit = 0; | |
int count = 0; | |
for(int i=0; i<n; i++) { | |
if(slots[i] == -1) continue; | |
totalProfit += slots[i]; | |
count += 1; | |
} | |
return {count, totalProfit}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment