Last active
May 21, 2021 22:00
-
-
Save Kellswork/9e4e8ba34c69618c29da27f5ee5c2297 to your computer and use it in GitHub Desktop.
Leetcode 121: Best Time to Buy and Sell Stock JavaScript solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Solution using Brute Force approach */ | |
function maxProfit(prices) { | |
// set maxProfit to zero, this helps for edge case that says return zero when no profit is made | |
let maxProfit = 0 | |
// we start the buying price at index 0 to get the prices[0] | |
for(let buyPrice = 0; buyPrice < prices.length; buy++) { | |
// start sell price with buyPrice + 1 to make sure we aren't going back on the days | |
// and to skip repeated calculations | |
for(let sell = buyPrice + 1; sell < prices.length; sell++) { | |
let profit = prices[sellPrice] - prices[buyPrice] | |
maxProfit = Math.max(maxProfit, profit) | |
} | |
} | |
return maxProfit | |
}; | |
/* | |
***Complexity analysis*** | |
- Time complexity: O(n^2). where n is the length of the prices array | |
- Space complexity: O(1). only two variables were created. maxProfit and profit | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Input: prices = [7,1,5,3,6,4] | |
Output: 5 | |
Explanation: | |
- prices = 7, 1, 5, 3, 6, 4 | |
- days = 1, 2, 3, 4, 5, 6 | |
- index = 0, 1, 2, 3, 4, 5 | |
you can see the days are index + 1. | |
you cannot buy from day 2 at price 1 and sell at day 1 at price 7, | |
you can only buy from day 1 at price 7 and sell on day 2 at price 1 or | |
sell on day 3 at price 5 and so on | |
so using the input as an example | |
prices = [ 7, 1, 5, 3, 6, 4 ] | |
starting with buying price = prices[0] and selling price = prices[1] | |
buyPrice = 7 sellPrice = 1, 5, 3, 6, 4 | |
sellPrice - buyPrice = profit | |
starting at day 1( index 0) we subtract the values at each index after index 0 to get the maximum profit we can find | |
1 - 7 = -6 | |
5 - 7 = -2 | |
3 - 7 = -4 | |
6 - 7 = -1 | |
4 - 7 = -3 | |
let maxProfit = 0 | |
maxProfit = Math.max(maxProfit, profit) // -1 | |
Math.max compares the values in maxProfit and profit, stores the maximum value between the both of the in the maxProfit variable | |
we do this for all the values in the array. | |
buyPrice = 1 sellPrice = 5, 3, 6, 4 | |
sellPrice - buyPrice = profit | |
starting at day 2( index 1) we subtract the values at each index after index 1 to get the maximum profit we can find | |
5 - 1 = 4 | |
3 - 1 = 2 | |
6 - 1 = 5 | |
4 - 1 = 3 | |
maxProfit = Math.max(maxProfit, profit) | |
// previous value => -1 | |
// new value => 5 | |
buyPrice = 5 sellPrice = 3, 6, 4 | |
profit = sellPrice - buyPrice | |
starting at day 3( index 2) we subtract the values at each index after index 2 to get the maximum profit we can find | |
3 - 5 = -2 | |
6 - 5 = 1 | |
4 - 5 = -1 | |
maxProfit = Math.max(maxProfit, profit) | |
// previous value => 5, value remains the same as we didn't get any value higher than 5 | |
buyPrice = 3 sellPrice = 6, 4 | |
sellPrice - buyPrice = profit | |
starting at day 4( index 3) we subtract the values at each index after index 3 to get the maximum profit we can find | |
6 - 3 = 3 | |
4 - 3 = 1 | |
maxProfit = Math.max(maxProfit, profit) | |
// previous value => 5, value remains the same as we didn't get any value higher than 5 | |
buyPrice = 6 sellPrice = 4 | |
sellPrice - buyPrice = profit | |
starting at day 5( index 4) we subtract the values at each index after index 4 to get the maximum profit we can find | |
4 - 6 = -2 | |
maxProfit = Math.max(maxProfit, profit) | |
// 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prices = [ 7, 1, 5, 3, 6, 4 ] // I removed prices[0] because this array focuses on the selling price | |
sP = 1 | |
mP = 7 | |
pr = -6 | |
is -6 > 0 // no so we leave it | |
mxP = 0 | |
is 1 < 7 // yes so | |
minPrice =. 1 | |
prices = [ 5, 3, 6, 4 ] | |
sP = 5 | |
mP = 1 | |
p = 4 | |
is 4 > 0 // yes so | |
mxP = 4 | |
is 5 < 1 // no so | |
mp remains 1 | |
prices = [ 3, 6, 4 ] | |
sP = 3 | |
mp = 1 | |
p = 2 | |
is 2 > 4 // no | |
mxP = 4 | |
is 3 < 1 // no | |
mp = 1 | |
prices = [ 6, 4 ] | |
sP = 6 | |
mP = 1 | |
p = 5 | |
is 5 > 4 // yes so we change | |
mxP = 5 | |
is 6 < 1 //nope so | |
minP = 1 // remains 1 | |
prices = [ 4 ] | |
sP = 4 | |
mp = 1 | |
p = 3 | |
is 3 > 5 // no mxP remains the same | |
mxP = 5 | |
is 4 < 1 // so minPrice remains the same | |
mP = 1 | |
//end of loop | |
MaxProfit = 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A better Approach using One For Loop; */ | |
function maxProfit(prices) { | |
let maxProfit = 0 | |
let minPrice = prices[0] | |
for(let sell = 1; sell < prices.length; sell++) { | |
let sellPrice = prices[sell] | |
let profit = sellPrice - minPrice | |
maxProfit = Math.max(maxProfit, profit) | |
if(sellPrice < minPrice) minPrice = sellPrice | |
} | |
return maxProfit | |
}; | |
/* Complexity analysis*** | |
- Time complexity: O(n) where n is the length of the prices array | |
- Space complexity: O(1). only two variables were created. maxProfit and profit | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
```jsx | |
Example 1: | |
Input: prices = [7,1,5,3,6,4] | |
Output: 5 | |
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. | |
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment