import sys sys.setrecursionlimit(2000000) def CLOSESTADJPRICE(listk): closeprise =…
- User
import sys sys.setrecursionlimit(2000000)
def CLOSEST_ADJ_PRICE(listk): close_prise = abs(int(listk[1]) - int(listk[0])) for i in range(len(listk) - 2): i += 1 new_prise = abs(int(listk[i + 1]) - int(listk[i])) if new_prise < close_prise: close_prise = new_prise print(str(close_prise))
def CLOSEST_PRICE(listk): g = sorted(listk) close_prise = int(g[1]) - int(g[0]) for i in range(len(listk) - 2): i += 1 new_prise = int(g[i + 1]) - int(g[i]) if new_prise < close_prise: close_prise = new_prise print(str(close_prise))
m = input().split() a1 = int(m[0]) a2 = int(m[1]) n = input().split() k = [] for i in range(len(n)): k.append(int(n[i])) for i in range(a2): l = input().split() if l[0] == 'BUY': k.append(int(l[1])) elif l[0] == 'CLOSEST_ADJ_PRICE': CLOSEST_ADJ_PRICE(k) else: CLOSEST_PRICE(k) 该程序的时间复杂度是?
- Assistant
该程序的时间复杂度是O(n),其中n为输入的元素个数。
- User
优化程序至时间复杂度至O(logn)
- Assistant
要将该程序的时间复杂度优化为O(logn),可以使用二分查找算法来寻找最接近的价格。