def CLOSESTADJPRICE(listk): closeprise = abs(int(listk[1]) - int(listk[0])) for i in…
- User
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) elif l[0] == ‘CLOSEST_PRICE’: CLOSEST_PRICE(k)改进该程序使它时间复杂度为O(logn)
- Assistant
To improve the program to have a time complexity of O(logn), we can use binary search instead of iterating through the list.
Here's an updated version of the code:
def closest_adj_price(prices): closest_price = float('inf') for i in range(len(prices)-1): price_diff = abs(prices[i+1] - prices[i]) closest_price = min(closest_price, price_diff) print(closest_price) def closest_price(prices): sorted_prices = sorted(prices) closest_price = float('inf') for i in range(len(sorted_prices)-1): price_diff = abs(sorted_prices[i+1] - sorted_prices[i]) closest_price = min(closest_price, price_diff) print(closest_price) def binary_search_closest(prices, target): low = 0 high = len(prices) - 1 closest_price = float('inf') while low <= high: mid = (low + high) // 2 if prices[mid] == target: return 0 elif prices[mid] < target: closest_price = min(closest_price, target - prices[mid]) low = mid + 1 else: closest_price = min(closest_price, prices[mid] - target) high = mid - 1 return closest_price 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) elif l[0] == 'CLOSEST_PRICE': closest_price(k) elif l[0] == 'CLOSEST_PRICE_BINARY': target = int(l[1]) closest = binary_search_closest(k, target) print(closest) - User
用中回答
- Assistant
要改进程序以将时间复杂度降低到O(logn),我们可以使用二分查找代替迭代整个列表。
这是代码的更新版本:
def closest_adj_price(prices): closest_price = float('inf') for i in range(len(prices)-1): price_diff = abs(prices[i+1] - prices[i]) closest_price = min(closest_price, price_diff) print(closest_price) def closest_price(prices): sorted_prices = sorted(prices) closest_price = float('inf') for i in range(len(sorted_prices)-1): price_diff = abs(sorted_prices[i+1] - sorted_prices[i]) closest_price = min(closest_price, price_diff) print(closest_price) def binary_search_closest(prices, target): low = 0 high = len(prices) - 1 closest_price = float('inf') while low <= high: mid = (low + high) // 2 if prices[mid] == target: return 0 elif prices[mid] < target: closest_price = min(closest_price, target - prices[mid]) low = mid + 1 else: closest_price = min(closest_price, prices[mid] - target) high = mid - 1 return closest_price 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) elif l[0] == 'CLOSEST_PRICE': closest_price(k) elif l[0] == 'CLOSEST_PRICE_BINARY': target = int(l[1]) closest = binary_search_closest(k, target) print(closest)