Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the efficient way to study in Python
Suppose we have three lists of equal length representing course assignments: deadlines, credits, and durations. For the i-th assignment, deadlines[i] shows its deadline, credits[i] shows its credit value, and durations[i] shows the days needed to complete it. We must complete one assignment before starting another, and we can finish an assignment on its due date. We start at day 0.
The goal is to find the maximum credits we can earn by efficiently scheduling assignments within their deadlines.
Example Problem
If we have deadlines = [7, 5, 10], credits = [8, 7, 10], and durations = [5, 4, 10], the maximum credits we can earn is 10.
Algorithm Approach
We use dynamic programming with the following steps ?
Sort assignments by deadlines to prioritize earlier deadlines
Use recursion to explore two choices: skip the current assignment or complete it (if possible)
Track the current day to ensure assignments finish within their deadlines
Return the maximum credits achievable
Implementation
class Solution:
def solve(self, deadlines, credits, durations):
# Sort assignments by deadlines (earliest first)
jobs = sorted(zip(deadlines, durations, credits))
def dp(i=0, day=0):
# Base case: no more assignments
if i >= len(jobs):
return 0
# Option 1: Skip current assignment
ans = dp(i + 1, day)
# Option 2: Complete current assignment (if deadline allows)
deadline, duration, credit = jobs[i]
if day + duration <= deadline:
ans = max(ans, dp(i + 1, day + duration) + credit)
return ans
return dp()
# Test the solution
ob = Solution()
deadlines = [7, 5, 10]
credits = [8, 7, 10]
durations = [5, 4, 10]
result = ob.solve(deadlines, credits, durations)
print(f"Maximum credits: {result}")
Maximum credits: 10
How It Works
The algorithm sorts assignments by deadlines, then uses recursion to explore all valid combinations. For each assignment, we decide whether to skip it or complete it (if the deadline permits). The function tracks the current day and returns the maximum credits possible.
In our example, we can complete the second assignment (deadline=5, duration=4, credit=7) from day 0 to day 4, then complete the third assignment (deadline=10, duration=10, credit=10) from day 4 to day 14. However, since day 14 exceeds deadline 10, we can only complete one assignment with credit 10.
Optimized Version with Memoization
class OptimizedSolution:
def solve(self, deadlines, credits, durations):
jobs = sorted(zip(deadlines, durations, credits))
memo = {}
def dp(i=0, day=0):
if (i, day) in memo:
return memo[(i, day)]
if i >= len(jobs):
return 0
# Skip current assignment
ans = dp(i + 1, day)
# Complete current assignment if possible
deadline, duration, credit = jobs[i]
if day + duration <= deadline:
ans = max(ans, dp(i + 1, day + duration) + credit)
memo[(i, day)] = ans
return ans
return dp()
# Test optimized version
ob = OptimizedSolution()
deadlines = [7, 5, 10]
credits = [8, 7, 10]
durations = [5, 4, 10]
result = ob.solve(deadlines, credits, durations)
print(f"Maximum credits (optimized): {result}")
Maximum credits (optimized): 10
Conclusion
This dynamic programming solution efficiently finds the maximum credits by exploring all valid assignment combinations. The memoized version improves performance by avoiding redundant calculations, making it suitable for larger datasets.
