2019.4 hello cycling face questions: list of stocks to buy and sell the data to find the optimum point
Ideas: the minimum recording scan index = i to time. diff is defined by the difference between the maximum point i; is fixed at a minimum value, the greater the value corresponding to our i, the larger the difference.
def maxbuns(array): if len(array)<2: return None # 在扫描到i时候,需要记录i之前的最小值 minNum = array[0] maxDiff = 0 for i in range(2,len(array)): if array[i-1]<minNum: minNum = array[i-1] diff = array[i]-minNum if diff > maxDiff: maxDiff = diff # return maxDiff buy_time = array.index(minNum) sell_time = array.index(maxDiff+minNum) return buy_time,sell_time,maxDiff if __name__ == "__main__": array = [9,11,8,5,7,12,16,14] print(maxbuns(array))