Find Receiver After K Seconds

Anonymous
c_cpp
07/20/2023 3:10 PM
792 B
21
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...