Eternoseeker

Ch-Assignment2-JobSequence

Nov 24th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | Source Code | 0 0
  1. def job_sequencing(jobs):
  2.     # sort jobs by profit in descending order
  3.     jobs.sort(key=lambda x: x[2], reverse=True)
  4.     # Calculate the maximum deadline  
  5.     max_deadline = max(jobs, key=lambda x: x[1])[1]
  6.     # To keep track of free time slots
  7.     result = [False] * max_deadline
  8.     total_profit = 0
  9.  
  10.     for job in jobs:
  11.         deadline = job[1] - 1
  12.         while deadline >= 0:
  13.             if not result[deadline]:
  14.                 result[deadline] = True
  15.                 total_profit += job[2]
  16.                 break
  17.             deadline -= 1
  18.  
  19.     return total_profit
  20.  
  21. jobs = [
  22.     (1, 5, 95),  
  23.     (2, 2, 15),  
  24.     (3, 7, 25),  
  25.     (4, 1, 40),  
  26.     (5, 5, 80),
  27.     (6, 4, 50),
  28.     (7, 2, 10),
  29.     (8, 3, 35),
  30.     (9, 4, 55)  
  31. ]
  32.  
  33. max_profit = job_sequencing(jobs)
  34. print("Maximum profit:", max_profit)
Add Comment
Please, Sign In to add comment