评判系统说这个解决方案给出了一个错误的答案,但我不明白为什么。任何见解都将不胜感激
num_test_cases = int(input())
def notOverlap(activity, arr):
# returns true if we have no overlapping activity in arr
for act in arr:
if not (act[0] >= activity[1] or act[1] <= activity[0]):
return False
return True
def decide(act, num_act):
C, J = [], []
result = [None]*num_act
for i in range(num_act):
if notOverlap(act[i], C):
C.append(act[i])
result[i] = "C"
elif notOverlap(act[i], J):
J.append(act[i])
result[i] = "J"
else:
return "IMPOSSIBLE"
return "".join(result)
for i in range(num_test_cases):
num_act = int(input())
act = []
for _ in range(num_act):
act.append(list(map(int, input().split())))
print("Case #" + str(i+1) + ": " + decide(act, num_act))
你用蛮力的方法解决了这个问题。您的代码运行缓慢,时间复杂度为O(N^2),但您可以在O(N*log(N))中完成它
不使用
notOverlap(activity, arr)
进行检查,对数组进行排序并使用上次结束时间活动C或J进行检查(贪婪的解决方法)在对数组进行排序之前,必须存储活动索引
这是我的解决方案,但在阅读解决方案之前,请尝试自己实现它
我希望你能从中受益匪浅,帮助你理解,并继续努力工作
相关问题 更多 >
编程相关推荐