def maxExecutedPrograms(n, m, k, time):
# Step 1: Sort the execution times of the programs time.sort()
executed_programs = 0 current_time_slot = 0 remaining_time_in_slot = k
# Step 2: Iterate over the sorted execution times for t in time:
# If we can fit the program in the current time slot, execute it if t <= remaining_time_in_slot:
executed_programs += 1 remaining_time_in_slot -= t
else:
# If we cannot fit it, move to the next time slot current_time_slot += 1 if current_time_slot >= m:
# No more time slots available, break out break # Use the next time slot and reset the remaining time remaining_time_in_slot = k - t
executed_programs += 1 return executed_programs
# Example usage:n = 5 # Number of programsm = 3 # Number of time slotsk = 10 # Duration of each time slot (seconds)time = [8, 3, 5, 7, 2] # Time required by each program# Output the maximum number of programs that can be executedresult = maxExecutedPrograms(n, m, k, time)
print(result) # Expected output: 4
OOR
def maxProgramsExecuted(n, m, k, times):
# Sort the programs by their execution times times.sort()
program_count = 0 current_slot_time = 0 slot_used = 0 # Iterate through each program for time in times:
# Check if we can fit the program into the current slot if current_slot_time + time <= k:
current_slot_time += time # Add program time to current slot program_count += 1 else:
# Move to the next slot slot_used += 1 if slot_used >= m:
# No more time slots available break # Reset the current slot time for the new slot and add the program current_slot_time = time
program_count += 1 return program_count