We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In this code there's a loop of O(n) to read the input, then another loop of O(n) to get the marks, then set() (I'm not sure but I think) is O(n), the sorted marks is O(nlgn) (here n being the size of the set), then another sorted() by names O(nlgn) and a O(n) loop to get the names with the second highest and print them.
A solution:
In the same loop that reads the input you could find out the 2nd lowest.
And instead of using a list to store the data use a dict of lists, where the key is the mark, and append to it the names.
After reading all the input you know the 2nd lowest mark, asking the dict for the data of that key will get you the list of names, sort them and print them.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nested Lists
You are viewing a single comment's thread. Return to all comments →
Short code does not equals efficient code.
In this code there's a loop of O(n) to read the input, then another loop of O(n) to get the marks, then set() (I'm not sure but I think) is O(n), the sorted marks is O(nlgn) (here n being the size of the set), then another sorted() by names O(nlgn) and a O(n) loop to get the names with the second highest and print them.
A solution: In the same loop that reads the input you could find out the 2nd lowest.
And instead of using a list to store the data use a dict of lists, where the key is the mark, and append to it the names.
After reading all the input you know the 2nd lowest mark, asking the dict for the data of that key will get you the list of names, sort them and print them.