Description
You and your friends are seizing another summer day, passing around the frisbee in the park. You have the most fun when the game is fair, so as much as possible, you would all like to ensure that everyone receives the disc an equal number of times. You also like to challenge yourselves, so when possible, you'll go for the longest pass you can throw.
So each friend will throw according to the following rules:
- Pass to the friend who has held the frisbee the least amount of times.
- In the event of a tie, pass to the furthest among these friends.
- If there's also a tie for furthest, pass to the one of these friends with the lowest index.
Given an array friends containing info about each player (where they're standing and how far they can throw), as well as the index of the starting player and the total number of passes, your task is to find which player will be holding the frisbee after all the passes are complete.
NOTE: Because you and your friends are really good at frisbee, it's safe to assume that all passes will be completed successfully; none of you will fumble a throw or catch.
Example
For friends = [[152, 213, 276], [274, 259, 151], [40, 57, 130], [203, 87, 189], [43, 182, 163]]
, numberOfPasses = 19
, and startingPlayer = 4
, the output should be frisbees(friends, numberOfPasses, startingPlayer) = 4
Visual example
Constraints
2 ≤ friends.length ≤ 5000
friends[i].length = 3
0 ≤ friends[i][0] ≤ 400
0 ≤ friends[i][1] ≤ 400
0 ≤ friends[i][2] ≤ 500
0 ≤ numberOfPasses ≤ 2500
0 ≤ startingPlayer < friends.length
Code
def make_player_throws_list(friends):
distance = lambda f, s: (f[0] - s[0])**2 + (f[1] - s[1])**2
return [
[
(i, distance(f, s))
for i, f in enumerate(friends)
if distance(f, s) <= s[2]**2 and f != s
]
for s in friends
]
def frisbees(friends, number_of_passes, starting_player):
frisbee_held = {i: 0 for i in range(len(friends))}
player_possible_throws_list = make_player_throws_list(friends)
for _ in range(number_of_passes):
frisbee_held[starting_player] += 1
starting_player = min(
player_possible_throws_list[starting_player],
key = lambda x: (frisbee_held[x[0]], -x[1], x[0])
)[0]
return starting_player