{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

121. Best Time to Buy and Sell Stock

\n", "
\n", "\n", "\n", "

Say you have an array for which the ith element is the price of a given stock on day i.

\n", "\n", "

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

\n", "\n", "

Note that you cannot sell a stock before you buy one.

\n", "\n", "

Example 1:

\n", "\n", "
Input: [7,1,5,3,6,4]\n",
    "Output: 5\n",
    "Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.\n",
    "             Not 7-1 = 6, as selling price needs to be larger than buying price.\n",
    "
\n", "\n", "

Example 2:

\n", "\n", "
Input: [7,6,4,3,1]\n",
    "Output: 0\n",
    "Explanation: In this case, no transaction is done, i.e. max profit = 0.\n",
    "
\n", "\n", "\n", "

 

\n", "Source \n", "
\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def max_profit(prices):\n", " if not prices:\n", " return 0\n", " profit = 0\n", " min_price = prices[0]\n", " for price in prices[1:]:\n", " if price - min_price > profit:\n", " profit = price - min_price\n", " if price < min_price:\n", " min_price = price\n", " return profit" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def max_profit(prices):\n", " \"\"\"small variation\"\"\"\n", " if not prices:\n", " return 0\n", " max_profit = 0\n", " min_price = prices[0]\n", " for price in prices[1:]:\n", " if price < min_price:\n", " min_price = price\n", " profit = price - min_price\n", " if profit > max_profit:\n", " max_profit = profit\n", " return max_profit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Check

" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prices = [7,1,5,3,6,4]\n", "max_profit(prices)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prices = [7,6,4,3,1]\n", "max_profit(prices)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prices = [4, 6, 3, 8, 0, 6]\n", "max_profit(prices)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prices = [4, 12, 0, 4]\n", "max_profit(prices)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }