首页 > 解决方案 > 改进嵌套循环搜索

问题描述

所以这来自我对嵌套循环所做的一个黑客等级问题: https ://www.hackerrank.com/challenges/nested-list/problem

我的解决方案通过了所有测试用例,但我想知道如何提高效率。即如何减少运行时间和内存分配。

if __name__ == '__main__':
    test = []
    second = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        test.append([name,score])
        second.append(score)

test = sorted(test, key=lambda x:x[1], reverse =True)
second = sorted(list(set(second)))
second = second[1]
out = [student[0] for student in test if student[1]==second]
out = sorted(out)
[print(x) for x in out]

标签: pythonperformancesorting

解决方案


推荐阅读