Leetcode - Arrays | Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock[E]
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Description
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1:
1 | Input: [7,1,5,3,6,4] |
Example 2:
1 | Input: [7,6,4,3,1] |
Solution
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/
1 | class Solution: |
Approach 1: Brute Force
We need to find out the maximum difference (which will be the maximum profit) between two numbers in the given array. Also, the second number (selling price) must be larger than the first one (buying price).
In formal terms, we need to find $$\max(\text{prices[j]} - \text{prices[i]})$$, for every i and j such that j > i.
1 | /** |
Complexity Analysis
- Time complexity : $$O(n^2)$$. Loop runs $$\dfrac{n (n-1)}{2}$$ times.
- Space complexity : $$O(1)$$ Only two variables $$ \text{maxprofit}$$ and $$\text{profit}$$ are used.
Approach 2: One Pass
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/
The points of interest are the peaks and valleys. We need to find the largest peak following the smallest valley. We can maintain two variables - minprice and maxprofit corresponding to the smallest valley and maximum profit (maximum difference between selling price and minprice) obtained so far respectively.
1 | /** |
- Time complexity : O(n). Only a single pass is needed.
- Space complexity : O(1). Only two variables are used.
122. Best Time to Buy and Sell Stock II[E]
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Description
Say you have an array prices for which the ith element is the price of a given stock on day i.
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).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
1 | Input: [7,1,5,3,6,4] |
Example 2:
1 | Input: [1,2,3,4,5] |
Example 3:
1 | Input: [7,6,4,3,1] |
Constraints:
1 <= prices.length <= 3 * 10 ^ 40 <= prices[i] <= 10 ^ 4
Solution
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/
1 | class Solution: |
123. Best Time to Buy and Sell Stock III[H]
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
Description
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
1 | Input: [3,3,5,0,0,3,1,4] |
Example 2:
1 | Input: [1,2,3,4,5] |
Example 3:
1 | Input: [7,6,4,3,1] |
Solution
https://discuss.leetcode.com/topic/50360/python-dp-and-non-dp-solution
1 | class Solution: |
188. Best Time to Buy and Sell Stock IV[H]
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Description
Say you have an array for which the *i-*th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example 1:
1 | Input: [2,4,1], k = 2 |
Example 2:
1 | Input: [3,2,6,5,0,3], k = 2 |
Solution
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/solution/
1 |
309. Best Time to Buy and Sell Stock with Cooldown[E]
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
Description
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
- After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
1 | Input: prices = [1,2,3,0,2] |
Example 2:
1 | Input: prices = [1] |
Constraints:
1 <= prices.length <= 50000 <= prices[i] <= 1000
Solution
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/solution/
1 |





