Skip to content

Commit 5e30125

Browse files
committed
121. Best Time to Buy and Sell Stock11'
1 parent 03b48f6 commit 5e30125

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
let use initialize Left and Right pointer to first and second position of array
34+
Here Left is to buy stock and Right is to sell stock
35+
36+
Then we initialize our max_profit as 0.
37+
38+
Now we will start our while loop and we will run till our
39+
Right pointer less then length of array
40+
For Example:
41+
prices=[7,1,5,3,6,4]
42+
Note:
43+
prices[left] --> buy stock
44+
prices[right] --> sell stock
45+
now we will check price at right and left pointer
46+
47+
step 1:
48+
49+
price[left]=7 price[right]=1 profit=-6
50+
here price[left] is greater than price[right] so we will move left pointer to the right position and increment our right pointer by 1. We always want our left point to be minimum
51+
52+
step 2:
53+
54+
price[left]=1 price[right]=5 profit=4
55+
here price[left] is less than price[right] which means we will get profit so we will update our max_profit and move our right pointer alone
56+
57+
step 3:
58+
59+
price[left]=1 price[right]=3 profit=2
60+
here price[left] is less than price[right] which means we will get profit so we will check our max_profit previously it
61+
62+
was 4 now our current profit is 2 so we will check which is maximum and update our max_profit and move our right pointer alone
63+
64+
step 4:
65+
66+
price[left]=1 price[right]=6 profit=5
67+
here price[left] is less than price[right] which means we will get profit so we will check our max_profit previously it was 4 now our current profit is 5 so we will check which is maximum and update our max_profit and move our right pointer alone
68+
69+
step 5:
70+
71+
price[left]=1 price[right]=4 profit=3
72+
same logic as above
73+
*/
74+
75+
const maxProfit = (prices) => {
76+
let left = 0; // Buy
77+
let right = 1; // sell
78+
let max_profit = 0;
79+
while (right < prices.length) {
80+
if (prices[left] < prices[right]) {
81+
let profit = prices[right] - prices[left]; // our current profit
82+
83+
max_profit = Math.max(max_profit, profit);
84+
} else {
85+
left = right;
86+
}
87+
right++;
88+
}
89+
return max_profit;
90+
};
91+
92+
module.exports.maxProfit = maxProfit;
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

+1
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)