Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
| [Gas Station](/LeetcodeProblems/Algorithms/medium/GasStation/index.js) | Medium | https://leetcode.com/problems/gas-station/description/ |
| [K Closest Points to Origin](/LeetcodeProblems/Algorithms/medium/K_Closest_Points_to_Origin.js/) | Medium | https://leetcode.com/problems/k-closest-points-to-origin/
| [BestTimeToBuy](LeetcodeProblems/Algorithms/easy/Best_Time_To_Buy_And_Sell_Stock_II.js) | Medium | https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii |
| [Best Time to Buy and Sell Stock](/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js) | Easy | https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
| [Flood Fill ](/LeetcodeProblems/Algorithms/easy/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/easy/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/easy/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp