Similar Problems
Similar Problems not available
Minimum Hours Of Training To Win A Competition - Leetcode Solution
Companies:
LeetCode: Minimum Hours Of Training To Win A Competition Leetcode Solution
Difficulty: Easy
Problem Statement: Given a list of n competitors, where each competitor is represented by a list of m integers, representing the hours of training that the competitor has completed in each of the m skills. You are also given a list of m integers, representing the minimum number of hours of training required to win the competition in each skill. A competitor can only win the competition if they have completed at least the minimum number of hours of training in all of the skills.
Write a function that returns the minimum number of hours of training that a competitor must complete in order to win the competition. If it is impossible for any competitor to win the competition, return -1.
Solution: To solve this problem, we can start by iterating through each competitor and checking if they have completed at least the minimum number of hours of training in all of the skills. If a competitor meets this requirement, we can calculate the total number of hours of training they have completed and compare it to the current minimum.
If a competitor has not completed the required number of hours in any skill, we cannot consider them as a potential winner and move on to the next competitor.
If at least one competitor meets the requirement, we can return the minimum number of hours of training required to win the competition. Otherwise, we return -1 to indicate that it is impossible for any competitor to win the competition.
Let's implement this solution in Python:
def min_hours_of_training(competitors, min_hours): n = len(competitors) m = len(min_hours)
min_total_hours = float('inf')
found_winner = False
for i in range(n):
total_hours = 0
for j in range(m):
if competitors[i][j] < min_hours[j]:
break
total_hours += competitors[i][j]
else:
found_winner = True
min_total_hours = min(min_total_hours, total_hours)
if found_winner:
return min_total_hours
else:
return -1
We start by initializing min_total_hours to infinity and found_winner to False. We then iterate through each competitor using a nested loop.
In the inner loop, we check if the current competitor has completed at least the minimum number of hours of training in each skill. If the competitor has not completed the required number of hours in any skill, we break out of the loop and move on to the next competitor.
If the competitor has completed the required number of hours in all skills, we calculate the total number of hours of training and update min_total_hours if necessary.
After iterating through all competitors, we check if we have found a potential winner by checking if found_winner is True. If we have found a winner, we return the minimum number of hours of training required. Otherwise, we return -1 to indicate that it is impossible for any competitor to win the competition.
Let's test the function with some sample inputs:
competitors = [ [2, 3, 4], [1, 2, 3], [3, 4, 5], [4, 5, 6] ] min_hours = [2, 3, 4]
print(min_hours_of_training(competitors, min_hours)) # Output: 12
In this example, the first competitor has completed at least the minimum number of hours in all skills, so we calculate their total hours as 9. The second competitor has not completed the required number of hours in the first skill, so we do not consider them. The third and fourth competitors both have completed at least the minimum number of hours in all skills, so we calculate their total hours as 12. Since 12 is less than infinity, we update min_total_hours to 12. The function returns 12 as the minimum number of hours of training required to win the competition.
Let's try another example where it is impossible for any competitor to win the competition:
competitors = [ [1, 2, 3], [2, 3, 4], [3, 4, 5] ] min_hours = [4, 5, 6]
print(min_hours_of_training(competitors, min_hours)) # Output: -1
In this example, no competitor has completed the required number of hours in at least one skill, so it is impossible for any competitor to win the competition. The function returns -1 to indicate this.
Minimum Hours Of Training To Win A Competition Solution Code
1