|
| 1 | +def get_max_profit(prices): |
| 2 | + """ Finds the maximum profit for buying and selling stock within a day. |
| 3 | +
|
| 4 | + >>> stock_prices_yesterday = [10, 7, 5, 8, 11, 9] |
| 5 | + >>> get_max_profit(stock_prices_yesterday) |
| 6 | + 6 |
| 7 | +
|
| 8 | + >>> stock_prices_yesterday = [10, 3] |
| 9 | + >>> get_max_profit(stock_prices_yesterday) |
| 10 | + -7 |
| 11 | +
|
| 12 | + >>> stock_prices_yesterday = [1, 10, 7, 14, 2, 11] |
| 13 | + >>> get_max_profit(stock_prices_yesterday) |
| 14 | + 13 |
| 15 | +
|
| 16 | + >>> stock_prices_yesterday = [11, 10, 9, 8, 2, 1] |
| 17 | + >>> get_max_profit(stock_prices_yesterday) |
| 18 | + -1 |
| 19 | +
|
| 20 | + >>> stock_prices_yesterday = [11, 9, 5, 2, 2, 0] |
| 21 | + >>> get_max_profit(stock_prices_yesterday) |
| 22 | + 0 |
| 23 | +
|
| 24 | + >>> stock_prices_yesterday = [1, 1, 1, 1, 1, 1] |
| 25 | + >>> get_max_profit(stock_prices_yesterday) |
| 26 | + 0 |
| 27 | + """ |
| 28 | + |
| 29 | + # Runtime: O(n^2) |
| 30 | + # Spacetime: O(n) |
| 31 | + |
| 32 | + |
| 33 | + max_profit = prices[1] - prices[0] |
| 34 | + |
| 35 | + for a in range(len(prices)): # O(n) |
| 36 | + for b in range(a + 1, len(prices)): # O(n) |
| 37 | + gain_loss = prices[b] - prices[a] |
| 38 | + if gain_loss > max_profit: |
| 39 | + max_profit = gain_loss |
| 40 | + print max_profit |
| 41 | + |
| 42 | + |
| 43 | +def get_max_profit_optimized(prices): |
| 44 | + """ Finds the maximum profit for buying and selling stock within a day. |
| 45 | +
|
| 46 | + >>> stock_prices_yesterday = [10, 7, 5, 8, 11, 9] |
| 47 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 48 | + 6 |
| 49 | +
|
| 50 | + >>> stock_prices_yesterday = [10, 3] |
| 51 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 52 | + -7 |
| 53 | +
|
| 54 | + >>> stock_prices_yesterday = [1, 10, 7, 14, 2, 11] |
| 55 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 56 | + 13 |
| 57 | +
|
| 58 | + >>> stock_prices_yesterday = [11, 10, 9, 8, 2, 1] |
| 59 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 60 | + -1 |
| 61 | +
|
| 62 | + >>> stock_prices_yesterday = [11, 9, 5, 2, 2, 0] |
| 63 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 64 | + 0 |
| 65 | +
|
| 66 | + >>> stock_prices_yesterday = [1, 1, 1, 1, 1, 1] |
| 67 | + >>> get_max_profit_optimized(stock_prices_yesterday) |
| 68 | + 0 |
| 69 | + """ |
| 70 | + |
| 71 | + |
| 72 | + # Runtime: O(n) |
| 73 | + # Spacetime: O(1) |
| 74 | + |
| 75 | + |
| 76 | + max_profit = prices[1] - prices[0] |
| 77 | + low = prices[0] |
| 78 | + |
| 79 | + for i in prices: |
| 80 | + |
| 81 | + # Skip the first price in list |
| 82 | + if prices[0] == i: |
| 83 | + continue |
| 84 | + |
| 85 | + potential_profit = i - low |
| 86 | + max_profit= max(max_profit, potential_profit) |
| 87 | + low = min(low, i) |
| 88 | + |
| 89 | + print max_profit |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + import doctest |
| 94 | + results = doctest.testmod() |
| 95 | + |
| 96 | + if results.failed == 0: |
| 97 | + print "ALL TESTS PASSED" |
0 commit comments