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 ff2ced0

Browse files
Job Sequencing, A Greedy Approach
How job is sequenced and calculates max profit using Greedy Approach? # Explained with code and comments
1 parent dd59bfb commit ff2ced0

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

‎Job Sequencing: Greedy

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Question: https://practice.geeksforgeeks.org/problems/job-sequencing-problem/0
2+
3+
class Job:
4+
def __init__(self, id_, deadline, profit):
5+
# making a job object that contains job details
6+
self.id_ = id_
7+
self.deadline = deadline
8+
self.profit = profit
9+
10+
def __lt__(self, other):
11+
# whenever lesser than(in sorting) is used,
12+
# it compares self.profit and other.profit to sort
13+
# based on profit
14+
return self.profit<other.profit
15+
16+
def __gt__(self, other):
17+
# to take into account, the highest number of
18+
# deadline time to make an array of that length
19+
# in order to track the deadlines fulfilled, while
20+
# using max(list) or comparing for greater
21+
return self.deadline>other.deadline
22+
23+
class JobSequencing:
24+
def max_profit(self, input_arr):
25+
# a list to store job objects
26+
job_list = list()
27+
# setting index for input_arr as 0, to iterate
28+
# we can also say, it's an iter variable
29+
index = 0
30+
while index<len(input_arr):
31+
# making job objects
32+
id_, deadline, profit = (input_arr[index],
33+
input_arr[index+1],
34+
input_arr[index+2])
35+
job_list.append(Job(id_, deadline, profit))
36+
index += 3
37+
# sorting job_list, a list of job objects
38+
# __lt__ method of Job class is for this purpose
39+
job_list.sort(reverse=True)
40+
# keeping account of total profit
41+
total_profit = 0
42+
# declaring a profit array to keep track of deadlines
43+
# len is the highest/max deadline present in job_list
44+
# __gt__ method of Job class is for max(list) purpose
45+
profit_array = [0]*max(job_list).deadline
46+
# setting a flag, profit, to ensure it's really a profit
47+
profit = True
48+
# to keep account of total jobs involved in making the profit
49+
no_of_jobs = 0
50+
# print("profit_array:", profit_array)
51+
for elem in job_list:
52+
# setting current as elem.deadline-1 to check
53+
# if it's 0, we will take it as profit else ignore it
54+
# based on flag profit
55+
current = elem.deadline - 1
56+
# print("elem.deadline:", elem.deadline)
57+
while profit_array[current]!=0 and current>-1:
58+
# print("current:", current)
59+
# to consider same deadlines accounting to profit
60+
# for multiple times, as- 2 200, 2 100
61+
current -= 1
62+
profit = False
63+
if current>-1:
64+
# possibility that there's no space to insert this new profit
65+
# if so, current==-1 and profit set to False
66+
# else, this condition evaluates to true and sets
67+
# profit=True
68+
profit = True
69+
if profit:
70+
# print("Current:", current)
71+
# if it's a profit, adding/considering it.
72+
profit_array[current] = elem.profit
73+
total_profit += elem.profit
74+
no_of_jobs += 1
75+
# print("profit array:", profit_array)
76+
print(no_of_jobs, total_profit)
77+
78+
for _ in range(int(input())):
79+
n = int(input())
80+
input_arr = list(map(int, input().split()))
81+
job_sequencing = JobSequencing()
82+
job_sequencing.max_profit(input_arr)
83+

0 commit comments

Comments
(0)

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