{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>122. Best Time to Buy and Sell Stock II</h1>\n",
    "<hr>\n",
    "\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "<p>Say you have an array <code>prices</code> for which the <em>i<sup>th</sup></em> element is the price of a given stock on day <em>i</em>.</p>\n",
    "\n",
    "<p>Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).</p>\n",
    "\n",
    "<p><strong>Note:</strong> You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).</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> 7\n",
    "<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.\n",
    "&nbsp;            Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.\n",
    "</pre>\n",
    "\n",
    "<p><strong>Example 2:</strong></p>\n",
    "\n",
    "<pre><strong>Input:</strong> [1,2,3,4,5]\n",
    "<strong>Output:</strong> 4\n",
    "<strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.\n",
    "&nbsp;            Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are\n",
    "&nbsp;            engaging multiple transactions at the same time. You must sell before buying again.\n",
    "</pre>\n",
    "\n",
    "<p><strong>Example 3:</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.</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<p><strong>Constraints:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= prices.length &lt;= 3 * 10 ^ 4</code></li>\n",
    "\t<li><code>0 &lt;= prices[i]&nbsp;&lt;= 10 ^ 4</code></li>\n",
    "</ul>\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<a href=\"https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/\">Source</a> \n",
    "<hr>\n",
    "\n",
    "<h4>Code</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def max_profit(prices):\n",
    "    profit = 0\n",
    "    for i in range(1, len(prices)):\n",
    "        if prices[i] >= prices[i-1]:\n",
    "            profit += prices[i] - prices[i-1]\n",
    "    return profit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def max_profit(prices):\n",
    "    \"\"\"small variation.\"\"\"\n",
    "    profit = 0\n",
    "    for i, price in enumerate(prices[1:], 1): # i starts at 1\n",
    "        if price >= prices[i-1]:\n",
    "            profit += price - prices[i-1]\n",
    "    return profit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Check</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "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": [
       "4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices = [1,2,3,4,5]\n",
    "max_profit(prices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices = [7,6,4,3,1]\n",
    "max_profit(prices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices = [4, 6, 3, 8, 0, 6]\n",
    "max_profit(prices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 7,
     "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
}