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 691ed78

Browse files
121. Best Time to Buy and Sell Stock
1 parent 5e30125 commit 691ed78

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

‎LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Approach:
3333
let use initialize Left and Right pointer to first and second position of array
3434
Here Left is to buy stock and Right is to sell stock
3535
36-
Then we initialize our max_profit as 0.
36+
Then we initialize our maxProfitValue as 0.
3737
3838
Now we will start our while loop and we will run till our
3939
Right pointer less then length of array
@@ -52,19 +52,19 @@ here price[left] is greater than price[right] so we will move left pointer to th
5252
step 2:
5353
5454
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
55+
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
5656
5757
step 3:
5858
5959
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
60+
here price[left] is less than price[right] which means we will get profit so we will check our maxProfitValue previously it
6161
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
62+
was 4 now our current profit is 2 so we will check which is maximum and update our maxProfitValue and move our right pointer alone
6363
6464
step 4:
6565
6666
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
67+
here price[left] is less than price[right] which means we will get profit so we will check our maxProfitValue previously it was 4 now our current profit is 5 so we will check which is maximum and update our maxProfitValue and move our right pointer alone
6868
6969
step 5:
7070
@@ -75,18 +75,18 @@ same logic as above
7575
const maxProfit = (prices) => {
7676
let left = 0; // Buy
7777
let right = 1; // sell
78-
let max_profit = 0;
78+
let maxProfitValue = 0;
7979
while (right < prices.length) {
8080
if (prices[left] < prices[right]) {
8181
let profit = prices[right] - prices[left]; // our current profit
8282

83-
max_profit = Math.max(max_profit, profit);
83+
maxProfitValue = Math.max(maxProfitValue, profit);
8484
} else {
8585
left = right;
8686
}
8787
right++;
8888
}
89-
return max_profit;
89+
return maxProfitValue;
9090
};
9191

9292
module.exports.maxProfit = maxProfit;

0 commit comments

Comments
(0)

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