Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c817ef

Browse files
Job Sequencing, A Greedy Approach
How to sequence a job to maximise profit using Greedy Approach? # Explained with code and comments
1 parent ff2ced0 commit 9c817ef

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

‎Job Sequencing: Greedy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Question: https://practice.geeksforgeeks.org/problems/job-sequencing-problem/0
22

3+
#code
4+
35
class Job:
46
def __init__(self, id_, deadline, profit):
57
# making a job object that contains job details
@@ -21,6 +23,8 @@ class Job:
2123
return self.deadline>other.deadline
2224

2325
class JobSequencing:
26+
"""Time: O(nlogn), Space: O(max(n, len(profit_array)))
27+
"""
2428
def max_profit(self, input_arr):
2529
# a list to store job objects
2630
job_list = list()
@@ -36,6 +40,7 @@ class JobSequencing:
3640
index += 3
3741
# sorting job_list, a list of job objects
3842
# __lt__ method of Job class is for this purpose
43+
# Time: O(nlogn) for sorting
3944
job_list.sort(reverse=True)
4045
# keeping account of total profit
4146
total_profit = 0
@@ -49,6 +54,7 @@ class JobSequencing:
4954
no_of_jobs = 0
5055
# print("profit_array:", profit_array)
5156
for elem in job_list:
57+
# Time: O(n) worst case
5258
# setting current as elem.deadline-1 to check
5359
# if it's 0, we will take it as profit else ignore it
5460
# based on flag profit

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /