mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Simplify loop in max profit challenge (#214)
This commit is contained in:
parent
2865bdcf94
commit
dff49256e5
|
@ -105,11 +105,9 @@
|
||||||
" raise TypeError('prices cannot be None')\n",
|
" raise TypeError('prices cannot be None')\n",
|
||||||
" if len(prices) < 2:\n",
|
" if len(prices) < 2:\n",
|
||||||
" raise ValueError('prices must have at least two values')\n",
|
" raise ValueError('prices must have at least two values')\n",
|
||||||
" min_price = prices[0]\n",
|
" min_price = prices.pop(0)\n",
|
||||||
" max_profit = -sys.maxsize\n",
|
" max_profit = prices[0] - min_price\n",
|
||||||
" for index, price in enumerate(prices):\n",
|
" for price in prices:\n",
|
||||||
" if index == 0:\n",
|
|
||||||
" continue\n",
|
|
||||||
" profit = price - min_price\n",
|
" profit = price - min_price\n",
|
||||||
" min_price = min(price, min_price)\n",
|
" min_price = min(price, min_price)\n",
|
||||||
" max_profit = max(profit, max_profit)\n",
|
" max_profit = max(profit, max_profit)\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user