-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[UPDATED]feat: add activity selection problem #2763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
chhavibansal
wants to merge
10
commits into
TheAlgorithms:master
from
chhavibansal:activity_selection
Closed
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20551f1
[Activity selection]
f744ce7
[Activity selection]
013a3d6
Upating activity selection
19814d9
Delete .idea/.gitignore
chhavibansal 883e427
Merge branch 'master' into activity_selection
chhavibansal 2582066
adding blanck line
5c3cca6
Delete .idea/workspace.xml
chhavibansal 0ae30f7
Merge branch 'master' into activity_selection
realstealthninja eb3e130
activity selection
e39a07a
activity selection
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
Problem Statement: | ||
You are given n activities with their start and finish times. Select the maximum number of activities | ||
that can be performed by a single person, assuming that a person can only work on a single activity at a time. | ||
Solution: | ||
Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than | ||
all other activities and start time is more than the previous activities finish time. We should sort the activities in | ||
increasing order of finish time of activities. | ||
*/ | ||
// The assumption here is input activities will be provided in sorted order of finish time of activities. | ||
#include <cassert> /// for assert | ||
#include <iostream> | ||
#include <vector> | ||
|
||
// Prints a maximum set of activities that can be done by a | ||
// single person, one at a time. | ||
std::vector<int> getMaxActivities(std::vector<int> start, std::vector<int> finish, int n) | ||
{ | ||
int i, j; | ||
std::vector<int> ans; | ||
|
||
|
||
// Since we greedily start selecting the activities the first activity is always selected | ||
i = 0; | ||
ans.push_back(i); | ||
|
||
// Considering the rest of the activities | ||
for (j = 1; j < n; j++) { | ||
// If this activity has start time greater than or | ||
// equal to the finish time of previously selected | ||
// activity, then select it | ||
if (start[j] >= finish[i]) { | ||
ans.push_back(j); | ||
i = j; | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
static void tests() { | ||
std::vector<int> start = {1, 3, 0, 5, 8, 5}; | ||
std::vector<int> finish = {2, 4, 6, 7, 9, 9}; | ||
|
||
std::vector<int> ans = getMaxActivities(start, finish, start.size()); | ||
std::vector<int> activities = {0, 1, 6, 4}; | ||
|
||
for(int i = 0 ; i < activities.size(); i++){ | ||
uint64_t activityNumber = ans[i]; | ||
assert(activityNumber == activities[i]); | ||
} | ||
} | ||
|
||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Driver code | ||
int main() | ||
{ | ||
//tests(); | ||
std::vector<int> start = {1, 3, 0, 5, 8, 5}; | ||
std::vector<int> finish = {2, 4, 6, 7, 9, 9}; | ||
// Function call | ||
std::vector<int> ans = getMaxActivities(start, finish, start.size()); | ||
std::cout << "Following activities are selected" << "\n"; | ||
for(int x: ans) { | ||
std::cout << x <<", "; | ||
} | ||
chhavibansal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.