In this post, we have Swift 5.0 solution for - Given the number of players P and the number of reserved courts C, returns the maximum number of games that can be played in parallel.
For Example, Given P = 5 players and C = 3 available courts, the function should return 2.
Working solution :
public func solution(_ P: Int, _ C: Int) -> Int {
// 1
if P < 2 || C < 1 {
return 0
}
// 2
return P >= (C*2) ? C : (P/2)%C
}
solution(5, 3) // returns 2
- When insufficient Players or Courts, games can not be played.
- If total players exceeds courts capacity, all courts occupied & games will be played. Otherwise return total number of games played from Players strength.
Full detail of exercise challenge :
Happy coding !