www.codingame.com
Write a program which, using a given number of strengths,
identifies the two closest strengths and shows their difference with an integer
信息
n = Number of horses
pi = strength of each horse
d = difference
1 < n < 100000
0 < pi ≤ 10000000
我的代碼目前
def get_dif(a, b):
return abs(a - b)
horse_str = [10, 5, 15, 17, 3, 8, 11, 28, 6, 55, 7]
n = len(horse_str)
d = 10000001
for x in range(len(horse_str)):
for y in range(x, len(horse_str) - 1):
d = min([get_dif(horse_str[x], horse_str[y + 1]), d])
print(d)
測試用例
[3,5,8, 9] outputs: 1
[10, 5, 15, 17, 3, 8, 11, 28, 6, 55, 7] outputs: 1
問題
他們都工作,但在以後的測試給了我一個很長的馬長處名單,我得到** Process has timed out. This may mean that your solution is not optimized enough to handle some cases.
哪有我優化它?謝謝!
EDIT ONE
默認代碼中給出
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
n = int(input())
for i in range(n):
pi = int(input())
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)
print("answer")
首先,這並不是冒泡。其次,你爲什麼要使用bubblesort? – user2357112
你可以使用python內建'sort'嗎? –
@ user2357112有用的意見將是不錯的 –