<jupyter_start><jupyter_text>Maximum Profit on SeriesGiven an array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it. Ex: the array $[9,11,8,5,7,10]$ should return 5.Solution idea: I think the idea is to get very comfortable with the easy case, which is the lowest price in an array is before the highest price in the array. But that's not necessarily the method that gets you the highest value. Ex: [9,20,8,9,10,10,5] should return 11 but the smallest number is obviously 5, which occurs at the end. If we change it to be [9,20,8,9,10,10] then here the smallest number is 8 and the the largest number after that is 10, but that would still be a lower profit than 11. So what to do... but what we've established is that looking for either the minimum or the maximum alone in an array won't necessarily give you the largest profit.   Brute force solution: ...just take each element and then compare it to the "profit" you'd make on each subsequent element. Then go down the list... The thing to remember of course is that if you have something like range(2,6) then it starts at 2 and goes up to 5. So range(2,6) --> 2,3,4,5 - Both of the<jupyter_code>def maxProfit(array): 
    '''
    maxProfit takes an array in list form
    Then it does a brute force search of the array and
    saves the value only if it's the biggest number it's found so far. 
    '''
    max_found = 0
    for i in range(len(array)): 
        for j in range(i+1, len(array)):
            profit = array[j] - array[i]
            if profit > max_found: 
                max_found = profit
        
    return max_found
print(maxProfit([9,11,8,5,7,10])) # 5
print(maxProfit([9,20,8,9,10,10,5])) # 11
print(maxProfit([9,20,8,9,10,10,22])) # 14<jupyter_output>5
11
14<jupyter_text>More clever? That approach takes O(N^2).. can we be a little more clever than the brute force solution? Well, what if you looked at the array backwards? Given that you need to buy the darn security first before you sell it. You need to find the highest price from the back of the array.<jupyter_code>def maxProfitClever(array): 
    '''
    look backwards, and keep track of the highest price seen
    then look at the current price and check how much profit 
    we'd have made buying at that price by comparing .. 
    the innovation here is that we can do it one pass! 
    '''
    current_max, max_found = 0, 0
    for price in reversed(array): 
        current_max = max(current_max, price) 
        potential_profit = current_max - price
        max_found = max(max_found, potential_profit) 
    return max_found
print(maxProfitClever([9,11,8,5,7,10]))
print(maxProfitClever([9,20,8,9,10,10,5]))
print(maxProfitClever([9,20,8,9,10,10,22]))<jupyter_output>5
11
14
{'fraction_non_alphanumeric': 0.08008284432171212, 'fraction_numerical': 0.042802899551259925, 'mean_word_length': 3.6666666666666665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '389075', 'license': 'MIT', 'n_tokens_mistral': 925, 'n_tokens_neox': 845, 'n_words': 408, 'path': 'Daily/Max Potential Series.ipynb', 'repo_name': 'jasonyum/python_go', 'size': '2897'}