python - Python最大成对产品时间限制超出错误
问题描述
n = int(input())
a = [int(x) for x in input().split()]
product = 0
for i in range(n):
for j in range(i + 1, n):
product = max(product, a[i] * a[j])
print(product)
当我将上述代码提交到 Corsera 的编码判断系统时,
Failed case #4/17: time limit exceeded (Time used: 9.98/5.00, memory used: 20918272/536870912.)
已被退回。我怎样才能改变它?
解决方案
它在 O(n^2) 中。您可以在 O(n log(n)) 中排序a
并选择两个较大的值a
作为结果(如果列表的输入值为a
正)。
input_list = sorted(a)
product = max(a[0]*a[1], a[-1] * a[-2]) #as suggested in comments if there is negative values
推荐阅读
- angular - SVG sanitization in Angular 7
- elasticsearch - Why does the Logstash ignore my configurations for filebeat
- spring-boot - Not able to Debug MicroService running inside docker container
- appium - Calender Datepicker in appium
- axapta - How to get Buttons (button name and button property) belong to a Form in D365 FO (AX)
- spring-boot - 许多相同的属性得到类似的实例列表
- c# - Is ExpandoObject thread-safe?
- android - How to trigger FireBase inappmessage at particular time when we are in app
- javascript - Dynamically added assets prefix in the image URL - Ember-CLI
- ios - Firebase fails to save data into database sometimes?