From 5e3012548480966f64efd39a1bf3e74d42006a33 Mon Sep 17 00:00:00 2001 From: Mukul Bansal Date: Sun, 20 Oct 2024 17:18:43 +0530 Subject: [PATCH 1/3] 121. Best Time to Buy and Sell Stock11' --- .../easy/Best_Time_to_buy_and_sell_stock.js | 92 +++++++++++++++++++ .../Best_Time_to_buy_and_sell_stock_Test.js | 15 +++ README.md | 1 + 3 files changed, 108 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js create mode 100644 LeetcodeProblemsTests/Algorithms/easy/Best_Time_to_buy_and_sell_stock_Test.js diff --git a/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js new file mode 100644 index 0000000..3f0d9c8 --- /dev/null +++ b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js @@ -0,0 +1,92 @@ +/* +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: +let use initialize Left and Right pointer to first and second position of array +Here Left is to buy stock and Right is to sell stock + +Then we initialize our max_profit as 0. + +Now we will start our while loop and we will run till our +Right pointer less then length of array +For Example: +prices=[7,1,5,3,6,4] +Note: +prices[left] --> buy stock +prices[right] --> sell stock +now we will check price at 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 left pointer to the right position and increment our right pointer by 1. We always want our left point to be 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 max_profit and move our right pointer alone + +step 3: + +price[left]=1 price[right]=3 profit=2 +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 2 so we will check which is maximum and update our max_profit and move our right pointer alone + +step 4: + +price[left]=1 price[right]=6 profit=5 +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 + +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 max_profit = 0; + while (right < prices.length) { + if (prices[left] < prices[right]) { + let profit = prices[right] - prices[left]; // our current profit + + max_profit = Math.max(max_profit, profit); + } else { + left = right; + } + right++; + } + return max_profit; +}; + +module.exports.maxProfit = maxProfit; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/easy/Best_Time_to_buy_and_sell_stock_Test.js b/LeetcodeProblemsTests/Algorithms/easy/Best_Time_to_buy_and_sell_stock_Test.js new file mode 100644 index 0000000..db97988 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/easy/Best_Time_to_buy_and_sell_stock_Test.js @@ -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; \ No newline at end of file diff --git a/README.md b/README.md index d03fae8..d820f21 100644 --- a/README.md +++ b/README.md @@ -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/ | From 691ed78c1e588a54c6cf42f8b7324749aff6151d Mon Sep 17 00:00:00 2001 From: Mukul Bansal Date: Sun, 20 Oct 2024 20:58:54 +0530 Subject: [PATCH 2/3] 121. Best Time to Buy and Sell Stock --- .../easy/Best_Time_to_buy_and_sell_stock.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js index 3f0d9c8..b5e67e9 100644 --- a/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js +++ b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js @@ -33,7 +33,7 @@ Approach: let use initialize Left and Right pointer to first and second position of array Here Left is to buy stock and Right is to sell stock -Then we initialize our max_profit as 0. +Then we initialize our maxProfitValue as 0. Now we will start our while loop and we will run till our 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 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 max_profit and move our right pointer alone +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] which means we will get profit so we will check our max_profit previously it +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 2 so we will check which is maximum and update our max_profit and move our right pointer alone +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 step 4: price[left]=1 price[right]=6 profit=5 -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 +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 step 5: @@ -75,18 +75,18 @@ same logic as above const maxProfit = (prices) => { let left = 0; // Buy let right = 1; // sell - let max_profit = 0; + let maxProfitValue = 0; while (right < prices.length) { if (prices[left] < prices[right]) { let profit = prices[right] - prices[left]; // our current profit - max_profit = Math.max(max_profit, profit); + maxProfitValue = Math.max(maxProfitValue, profit); } else { left = right; } right++; } - return max_profit; + return maxProfitValue; }; module.exports.maxProfit = maxProfit; \ No newline at end of file From c13e3f6974d72c35f5efe063e361ac087b97db87 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Sun, 20 Oct 2024 20:33:14 -0300 Subject: [PATCH 3/3] Update Best_Time_to_buy_and_sell_stock.js --- .../easy/Best_Time_to_buy_and_sell_stock.js | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js index b5e67e9..a94d16b 100644 --- a/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js +++ b/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js @@ -30,41 +30,44 @@ Constraints: */ /* Approach: -let use initialize Left and Right pointer to first and second position of array -Here Left is to buy stock and Right is to sell stock +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 -Then we initialize our maxProfitValue as 0. +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. -Now we will start our while loop and we will run till our -Right pointer less then length of array For Example: prices=[7,1,5,3,6,4] Note: prices[left] --> buy stock prices[right] --> sell stock -now we will check price at right and left pointer +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 left pointer to the right position and increment our right pointer by 1. We always want our left point to be minimum +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 +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] which means we will get profit so we will check our maxProfitValue previously it - -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 +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 -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 +same logic as above step 5: @@ -89,4 +92,4 @@ const maxProfit = (prices) => { return maxProfitValue; }; -module.exports.maxProfit = maxProfit; \ No newline at end of file +module.exports.maxProfit = maxProfit;