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

Commit 77274c2

Browse files
121. Best Time to Buy and Sell Stock (#100)
2 parents 03b48f6 + c13e3f6 commit 77274c2

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
121. Best Time to Buy and Sell Stock
3+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
4+
5+
Problem:
6+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
7+
8+
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.
9+
10+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
11+
12+
13+
14+
Example 1:
15+
16+
Input: prices = [7,1,5,3,6,4]
17+
Output: 5
18+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
19+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
20+
Example 2:
21+
22+
Input: prices = [7,6,4,3,1]
23+
Output: 0
24+
Explanation: In this case, no transactions are done and the max profit = 0.
25+
26+
Constraints:
27+
28+
1 <= prices.length <= 10^5
29+
0 <= prices[i] <= 10^4
30+
*/
31+
/*
32+
Approach:
33+
We will use a Two pointers strategy (Left and Right pointers).
34+
The first will start pointing to the first element, and the right to the second position of array.
35+
The Left is to buy stock and Right is to sell stock
36+
37+
We initialize our maxProfitValue as 0.
38+
39+
Now we will start our while loop, and we will run till our
40+
Right pointer less than the array's length.
41+
42+
For Example:
43+
prices=[7,1,5,3,6,4]
44+
Note:
45+
prices[left] --> buy stock
46+
prices[right] --> sell stock
47+
We will check the price at the right and left pointer
48+
49+
step 1:
50+
51+
price[left]=7 price[right]=1 profit=-6
52+
here, price[left] is greater than price[right], so we will move the left pointer to the right position
53+
and increment our right pointer by 1. We always want our left point to be the minimum.
54+
55+
step 2:
56+
57+
price[left]=1 price[right]=5 profit=4
58+
here, price[left] is less than price[right], which means we will get profit,
59+
so we will update our maxProfitValue and move our right pointer alone
60+
61+
step 3:
62+
63+
price[left]=1 price[right]=3 profit=2
64+
here, price[left] is less than price[right], we will get profit, so we will compare the maxProfitValue with the current profit.
65+
We will update our maxProfitValue and move our right pointer alone
66+
67+
step 4:
68+
69+
price[left]=1 price[right]=6 profit=5
70+
same logic as above
71+
72+
step 5:
73+
74+
price[left]=1 price[right]=4 profit=3
75+
same logic as above
76+
*/
77+
78+
const maxProfit = (prices) => {
79+
let left = 0; // Buy
80+
let right = 1; // sell
81+
let maxProfitValue = 0;
82+
while (right < prices.length) {
83+
if (prices[left] < prices[right]) {
84+
let profit = prices[right] - prices[left]; // our current profit
85+
86+
maxProfitValue = Math.max(maxProfitValue, profit);
87+
} else {
88+
left = right;
89+
}
90+
right++;
91+
}
92+
return maxProfitValue;
93+
};
94+
95+
module.exports.maxProfit = maxProfit;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const assert = require("assert");
2+
const maxProfitEqual = require("../../../LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock").maxProfit;
3+
4+
function test() {
5+
assert.deepEqual(
6+
maxProfit([7,1,5,3,6,4]),
7+
5
8+
);
9+
assert.deepEqual(
10+
maxProfit([7,6,4,3,1]),
11+
0
12+
);
13+
}
14+
15+
module.exports.test = test;

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
7272
| [Gas Station](/LeetcodeProblems/Algorithms/medium/GasStation/index.js) | Medium | https://leetcode.com/problems/gas-station/description/ |
7373
| [K Closest Points to Origin](/LeetcodeProblems/Algorithms/medium/K_Closest_Points_to_Origin.js/) | Medium | https://leetcode.com/problems/k-closest-points-to-origin/
7474
| [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 |
75+
| [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/
7576
| [Flood Fill ](/LeetcodeProblems/Algorithms/easy/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
7677
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/easy/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
7778
| [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/ |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /