- Notifications
You must be signed in to change notification settings - Fork98
121. Best Time to Buy and Sell Stock#100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ignacio-chiazzo merged 3 commits intoignacio-chiazzo:masterfrommeetmukul1993:BuySellStockOct 20, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletionsLeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
121. Best Time to Buy and Sell Stock | ||
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ | ||
Problem: | ||
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: | ||
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. | ||
Example 2: | ||
Input: prices = [7,6,4,3,1] | ||
Output: 0 | ||
Explanation: In this case, no transactions are done and the max profit = 0. | ||
Constraints: | ||
1 <= prices.length <= 10^5 | ||
0 <= prices[i] <= 10^4 | ||
*/ | ||
/* | ||
Approach: | ||
We will use a Two pointers strategy (Left and Right pointers). | ||
The first will start pointing to the first element, and the right to the second position of array. | ||
The Left is to buy stock and Right is to sell stock | ||
We initialize our maxProfitValue as 0. | ||
Now we will start our while loop, and we will run till our | ||
Right pointer less than the array's length. | ||
For Example: | ||
prices=[7,1,5,3,6,4] | ||
Note: | ||
prices[left] --> buy stock | ||
prices[right] --> sell stock | ||
We will check the price at the right and left pointer | ||
step 1: | ||
price[left]=7 price[right]=1 profit=-6 | ||
here, price[left] is greater than price[right], so we will move the left pointer to the right position | ||
and increment our right pointer by 1. We always want our left point to be the minimum. | ||
step 2: | ||
price[left]=1 price[right]=5 profit=4 | ||
here, price[left] is less than price[right], which means we will get profit, | ||
so we will update our maxProfitValue and move our right pointer alone | ||
step 3: | ||
price[left]=1 price[right]=3 profit=2 | ||
here, price[left] is less than price[right], we will get profit, so we will compare the maxProfitValue with the current profit. | ||
We will update our maxProfitValue and move our right pointer alone | ||
step 4: | ||
price[left]=1 price[right]=6 profit=5 | ||
same logic as above | ||
step 5: | ||
price[left]=1 price[right]=4 profit=3 | ||
same logic as above | ||
*/ | ||
const maxProfit = (prices) => { | ||
let left = 0; // Buy | ||
let right = 1; // sell | ||
let maxProfitValue = 0; | ||
while (right < prices.length) { | ||
if (prices[left] < prices[right]) { | ||
let profit = prices[right] - prices[left]; // our current profit | ||
maxProfitValue = Math.max(maxProfitValue, profit); | ||
} else { | ||
left = right; | ||
} | ||
right++; | ||
} | ||
return maxProfitValue; | ||
}; | ||
module.exports.maxProfit = maxProfit; |
15 changes: 15 additions & 0 deletionsLeetcodeProblemsTests/Algorithms/easy/Best_Time_to_buy_and_sell_stock_Test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const assert = require("assert"); | ||
const maxProfitEqual = require("../../../LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock").maxProfit; | ||
function test() { | ||
assert.deepEqual( | ||
maxProfit([7,1,5,3,6,4]), | ||
5 | ||
); | ||
assert.deepEqual( | ||
maxProfit([7,6,4,3,1]), | ||
0 | ||
); | ||
} | ||
module.exports.test = test; |
1 change: 1 addition & 0 deletionsREADME.md
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.