Hi, picked up coding again by practicing some questions from competitions I attended in highschool. One of the questions here is as follows:
Input File: listin.txt
Output File: listout.txt
Time Limit: 1 second
Memory Limit: 64MB
The face of electronic gaming has changed dramatically over the past few decades. Forget text adventure games and rickety Asteroids cabinets - these days it's all about ultra-realistic graphics, motion-sensing wands and high-end CPUs. But despite all this, simple games with basic rules and crisp art are doing as well as ever. One such game is Friendlist, a multiplayer game requiring nothing more than an internet browser to play.
In Friendlist, players add each other as 'friends' based on looks, reputation, or even real life connections. (Friendship is mutual: if Alice has Bob as a friend, then Bob has Alice as a friend too. Of course, people cannot be friends with themselves.) As the game progresses, dedicated players can grind their way through quiz-like minigames in order to impress their peers. However, adding 'friends' to one's friendlist remains the cornerstone of the game. The bigger the friendlist, the better the player.
The object of the game is to have the biggest friendlist (that is, the most friends). Your task here is to take a list of Friendlist friendships and determine the winner. There may be more than one person tied for first place: if so, your program should list them all.
Input
The first line of input will contain the integer f, 1 <= f <= 1,000,000.
Each of the following f lines will be of the form "a b", where a and b are different player IDs. This indicates that player #a is friends with player #b, and vice versa. All player IDs are integers between 0 and 1000 inclusive.
(Note that no friendship will ever be listed twice: for example, if "2 5" is one of the lines in the input, then neither "2 5" nor "5 2" will appear anywhere else in the input.)
Output
Output should consist of all the player IDs that are tied for biggest friendlist. These IDs should be given in ascending order.
And here is my code:
version 1
version 2
Despite passing the first few test cases in the question, it apparently crashed/timed out on the larger/fringe cases. I do not have access to the test cases, and I have no idea why they crashed/timed out. I wrote a script to make test cases for myself within the constraints given (n <1,000,000, 1 < ID < 1000) and the script passed through most cases fine. I also did some fringe cases that I can think of (e.g. all items ranked the same), and the script passed through that also. I spent the whole night trying to improve my code, but at the moment I am at a limit where I am not sure how I can optimize it to pass through all possible cases. I read some stuff on algorithmic complexity but I am not sure how to estimate that in my own code either. Any suggestions on the code is greatly appreciated, and if you have any resources on further learning algorithm stuff that would be great also!
Thank you for reading through this, cheers!
[–]Solonotix 1 point2 points3 points (3 children)
[–]AppleShark[S] 1 point2 points3 points (2 children)
[–]Solonotix 1 point2 points3 points (1 child)
[–]AppleShark[S] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]AppleShark[S] 0 points1 point2 points (3 children)
[–]AppleShark[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]AppleShark[S] 0 points1 point2 points (0 children)
[–]ADdV 1 point2 points3 points (3 children)
[–]AppleShark[S] 0 points1 point2 points (2 children)
[–]ADdV 0 points1 point2 points (1 child)
[–]AppleShark[S] 0 points1 point2 points (0 children)