-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
[Add] : Job Sequence program under GREEDY methods #10482
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2320d90
to add job seq program
kosuri-indu 3b3f3a8
to add job seq program
kosuri-indu 83394f4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu 8bc4629
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a20527
to add definitions in parameters
kosuri-indu 9663d5d
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu f6c1223
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3aacb09
to add definitions in parameters
kosuri-indu 4f30ae4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu 7fe25bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c220965
to add definitions in parameters
kosuri-indu 349c324
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 58856c9
changes as recommended
kosuri-indu a036fd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 688fd27
type hint error resolved
kosuri-indu 0a651e4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu 37ad111
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82877b3
removed lambda
kosuri-indu 4697ade
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu f2ed330
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 148a08d
import stmts order
kosuri-indu c261d83
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu 20f0dd0
Update and rename job_sequence.py to job_sequence_with_deadline.py
cclauss 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,58 @@ | ||
""" | ||
Given a list of tasks, each with a deadline and reward, | ||
The maximum number of tasks that can be scheduled to maximize reward. | ||
We can only complete one task at a time, and each task takes 1 unit | ||
of time to complete. Once a task has passed its deadline, it can no | ||
longer be scheduled. | ||
|
||
Example : | ||
tasks_info = [(4, 20), (1, 10), (1, 40), (1, 30)] | ||
max_tasks will return (2, [2, 0]) - | ||
which is by scheduling the tasks with rewards 40, 20 | ||
|
||
This problem can be solved using the concept of "GREEDY ALGORITHM". | ||
Time Complexity - O(n log n) | ||
|
||
We iterate over the tasks array once, sorting it in descending order of reward. | ||
Then we iterate over the sorted tasks array once more, scheduling each | ||
task if its deadline is greater than the current time.The greedy choice at | ||
each point is to either schedule the current task if its deadline is greater | ||
than the current time, or skip it otherwise. | ||
""" | ||
|
||
|
||
class Task: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, id : int, deadline : int, reward : int): | ||
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.id = id | ||
self.deadline = deadline | ||
self.reward = reward | ||
|
||
|
||
def max_tasks(tasks_info: list[tuple[int]]) -> int: | ||
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)]) | ||
(2, [2, 0]) | ||
>>> max_tasks([(1, 10), (2, 20), (3, 30), (2, 40)]) | ||
(2, [3, 2]) | ||
""" | ||
tasks = [Task(i, d, p) for i, (d, p) in enumerate(tasks_info)] | ||
|
||
tasks.sort(key=lambda task: task.reward, reverse=True) | ||
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
schedule = [] | ||
current_time = 0 | ||
|
||
for task in tasks: | ||
if task.deadline > current_time: | ||
schedule.append(task.id) | ||
current_time += 1 | ||
|
||
return len(schedule), schedule | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
print(max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)])) |
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.