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
Find minimum time to finish all jobs with given constraints in Python
We need to find the minimum time to finish all jobs when we have k assignees and each assignee takes t time units per job unit. The key constraints are that each assignee can only work on contiguous jobs, and no job can be split between assignees.
The problem can be solved using binary search on the answer. We search for the minimum possible time limit such that all jobs can be completed by k assignees within that limit.
Algorithm Overview
The solution uses binary search between the minimum possible time (maximum single job) and maximum possible time (sum of all jobs). For each candidate time, we check if it's possible to distribute jobs among k assignees.
Helper Function: Checking Validity
The is_valid() function checks if all jobs can be completed within a given time limit ?
def is_valid(time, K, jobs):
n = len(jobs)
count = 1 # Number of assignees needed
curr_time = 0 # Current assignee's workload
i = 0
while i < n:
if curr_time + jobs[i] > time:
# Current assignee can't take this job, need new assignee
curr_time = 0
count += 1
else:
# Current assignee can take this job
curr_time += jobs[i]
i += 1
return count <= K # Check if we need <= K assignees
Main Function: Binary Search Solution
The main function performs binary search to find the optimal time limit ?
def get_minimum_time(K, T, jobs):
n = len(jobs)
# Binary search bounds
begin = max(jobs) # Minimum possible time (largest single job)
end = sum(jobs) # Maximum possible time (all jobs by one person)
result = end
while begin <= end:
mid = (begin + end) // 2
if is_valid(mid, K, jobs):
# This time works, try to find smaller time
result = min(result, mid)
end = mid - 1
else:
# This time doesn't work, need more time
begin = mid + 1
return result * T # Multiply by time per unit
# Helper function for validation
def is_valid(time, K, jobs):
n = len(jobs)
count = 1
curr_time = 0
i = 0
while i < n:
if curr_time + jobs[i] > time:
curr_time = 0
count += 1
else:
curr_time += jobs[i]
i += 1
return count <= K
Complete Example
Let's test the solution with the given example ?
def is_valid(time, K, jobs):
n = len(jobs)
count = 1
curr_time = 0
i = 0
while i < n:
if curr_time + jobs[i] > time:
curr_time = 0
count += 1
else:
curr_time += jobs[i]
i += 1
return count <= K
def get_minimum_time(K, T, jobs):
begin = max(jobs)
end = sum(jobs)
result = end
while begin <= end:
mid = (begin + end) // 2
if is_valid(mid, K, jobs):
result = min(result, mid)
end = mid - 1
else:
begin = mid + 1
return result * T
# Test case
jobs = [12, 6, 9, 15, 5, 9]
k = 4
T = 5
result = get_minimum_time(k, T, jobs)
print(f"Minimum time to complete all jobs: {result}")
# Show the optimal distribution
optimal_limit = result // T
print(f"Optimal time limit per assignee: {optimal_limit}")
Minimum time to complete all jobs: 75 Optimal time limit per assignee: 15
How the Algorithm Works
With jobs [12, 6, 9, 15, 5, 9], k=4, and T=5:
Binary search range: 15 (max job) to 56 (sum of all jobs)
Optimal time limit found: 15 units per assignee
Job distribution: [12], [6, 9], [15], [5, 9]
Each group takes ? 15 units, total time = 15 × 5 = 75
Time Complexity
The time complexity is O(n log(sum)) where n is the number of jobs and sum is the total of all job times. The binary search runs in O(log(sum)) iterations, and each validation takes O(n) time.
Conclusion
This binary search approach efficiently finds the minimum time by testing different time limits and checking feasibility. The key insight is that if a time limit works, we can try smaller limits; otherwise, we need larger limits.
