【发布时间】:2016-04-29 11:37:08
【问题描述】:
我为this problem.写了以下代码
prof = sorted([int(input()) for x in range(int(input()))])
student = sorted([int(input()) for x in range(int(input()))])
prof_dates = len(prof)
stud_dates = len(student)
amount = 0
prof_index = 0
stud_index = 0
while stud_index < stud_dates and prof_index < prof_dates:
if student[stud_index] == prof[prof_index]:
amount += 1
stud_index += 1
elif student[stud_index] > prof[prof_index]:
prof_index += 1
elif student[stud_index] < prof[prof_index]:
stud_index += 1
print(amount)
但代码会产生超出时间限制的错误。早些时候我曾尝试对学生中的每个项目使用in,但它产生了一个 TLE,我相信这是因为in 语句是O(n)。因此,我编写了这段代码,其所需的步骤大致等于两个列表的长度之和。但这也产生了 TLE。那么,我应该对我的代码进行哪些更改。是否有一些特别的部分需要花费大量的时间?
谢谢。
【问题讨论】:
-
由于代码正在运行,也许这更适合Code Review。
标签: python algorithm performance python-3.x