{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>121. Best Time to Buy and Sell Stock</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Say you have an array for which the <em>i</em><sup>th</sup> element is the price of a given stock on day <em>i</em>.</p>\n", "\n", "<p>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.</p>\n", "\n", "<p>Note that you cannot sell a stock before you buy one.</p>\n", "\n", "<p><strong>Example 1:</strong></p>\n", "\n", "<pre><strong>Input:</strong> [7,1,5,3,6,4]\n", "<strong>Output:</strong> 5\n", "<strong>Explanation:</strong> 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", "</pre>\n", "\n", "<p><strong>Example 2:</strong></p>\n", "\n", "<pre><strong>Input:</strong> [7,6,4,3,1]\n", "<strong>Output:</strong> 0\n", "<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.\n", "</pre>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/best-time-to-buy-and-sell-stock/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "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": [ "<h4>Check</h4>" ] }, { "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 }