Find Receiver After K Seconds

 avatar
unknown
c_cpp
2 years ago
594 B
9
Indexable
int getReceiver(vector<int> &receivers, int seconds){

    vector<int> actualPlayers;
    map<int, int> players;
    int player = 1;                                 // currrent player

    while(!players[player]){                        // ensures no looping occurs
        players[player]++;                          // register player
        actualPlayers.push_back(player);            // order of players receiving the ball first
        int nextPlayer = receivers[player-1]; 
        player = nextPlayer;
    }

    return actualPlayers[seconds%actualPlayers.size()];
    
}
Editor is loading...