(you are here)

Friendlist

Let's solve Friendlist.

Let's start by summarizing problem: You're given a list of friendship pairs and would like to print out the IDs of the players with the most friends, in ascending order.

What data should we store?

Firstly, we need to work out how we want to store the data. There are many options on what to do here. One important (but simple) realization is that we aren't interested in who is friends with who exactly, rather we are only interested in how many friendship pairs each person is part of. Thus, it makes sense for us to create a list nCount, where nCount[i] will store the number of friends that player i has. We can calculate this list as follows:

# TODO: Read in the integer f
for i in range(f):
    # TODO: Read in the i-th friendship pair a b
    nFriends[a] += 1
    nFriends[b] += 1
// TODO: Read in the integer f
for(int i = 0; i < f; i++) {
    // TODO: Read in the i-th friendship pair a b
    nFriends[a]++;
    nFriends[b]++;
}

It's always important to think through some of the edge cases:

After that, it's just a matter of calculating the maximum number of friends any player has, like we did in A Dish Best Served Cold, and then printing out the player IDs like in Triple Hunting!

Your turn!


Solve Friendlist to complete this tutorial.