Skip to content

Commit 7bbf0d3

Browse files
committed
Update buy sell stock problem
1 parent 64b7dfd commit 7bbf0d3

File tree

7 files changed

+65
-36
lines changed

7 files changed

+65
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ List of Programs related to data structures and algorithms
7474

7575
14. First missing positive number: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.md)
7676

77-
15. Best time to buy stock and sell stock: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/buySellStock/buySellStock.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/buySellStock/buySellStock.md)
77+
15. Best time to buy stock and sell stock: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.md)
7878

7979
### String
8080

src/java1/algorithms/array/buySellStock/BuySellStock.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22
//Greedy approach/Sliding window: TC:O(n), SC:O(1)
33

44
class BuySellStock {
5-
private static int maxProfit(int[] stockPrices) {
5+
private static int buySellStock(int[] stockPrices) {
66
if(stockPrices.length < 2) return 0;
77

8-
int maxProfit = 0, left=0;
9-
for(int right=1; right< stockPrices.length; right++) {
8+
int maxProfit = 0, left=0, right = 1;
9+
while(right< stockPrices.length) {
1010
if(stockPrices[right]>stockPrices[left]) {
1111
maxProfit = Math.max(maxProfit, stockPrices[right]-stockPrices[left]);
1212
} else {
1313
left = right;
1414
}
15+
right++;
1516
}
1617
return maxProfit;
1718
}
1819

1920
public static void main(String[] args) {
20-
int[] stockPrices = {8,3,6,4,7,5};
21-
System.out.println(maxProfit(stockPrices));
21+
int[] stockPrices1 = {8,3,6,4,7,5};
22+
System.out.println(buySellStock(stockPrices1));
23+
int[] stockPrices2 = {7, 6, 5, 4, 3, 2, 1};
24+
System.out.println(buySellStock(stockPrices2));
2225
}
2326
}
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1+
**Description:**
2+
Given an array `prices` where `prices[i]` is the price of a given stock on the `i`th day. The profit can be maximized 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, just return 0.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: prices = [8, 3, 6, 4, 7, 5]
8+
Output: 4
9+
10+
Input: prices = [7, 6, 5, 4, 3, 2, 1]
11+
Output: 0
12+
113
**Algorithmic Steps**
214
This problem is solved with the help of sliding window approach to calculate the maximum profit(as known as **greedy algorithm**). The algorithmic approach can be summarized as follows:
315

4-
1. Write a preliminary case by returning profit as `0` if the number of stock prices are less than 2.
16+
1. Write a preliminary case by returning profit as `0` if the number of stock prices in the array are less than 2.
517

6-
2. Initialize `maxProfit` variable as 0 to indicate maximum profit for buying and selling the stock at different days.
18+
2. Initialize `maxProfit` variable to `0` to indicate maximum profit achieved for buying and selling the stock at different days.
719

8-
3. Initialize `left` variable(left pointer) as 0, which denotes the buying stock index.
20+
3. Initialize left pointer(i.e,`left`) to `0`, which denotes the buying stock index.
921

10-
4. Iterate over the input stock prices and compare the current stock price(i.e, `stockPrices[right]`) with previous day stock price(i.e, `stockPrices[left]`). If the current stock price is higher, calculate the maximum profit between previous maxProfit and current stock difference.
22+
4. Iterate over the input stock prices and compare the current stock price(i.e, `stockPrices[right]`) with previous day stock price(i.e, `stockPrices[left]`). If the current stock price is higher, calculate the maximum profit between previous maxProfit and current profit.
1123

12-
5. If the current stock price is lower than previous stock price, consider the current stock price as buying stock with the help of their indexes in an array(i.e, `left = right`).
24+
5. If the current stock price is lower than previous stock price, consider the current stock price as buying stock with the help of their index pointers(i.e, `left = right`).
1325

1426
6. Return `maxProfit` which represents best time(or max proft scenario) to buy and sell stock prices.
1527

16-
1728
**Time and Space complexity:**
18-
This algorithm has a time complexity of O(n), where n is the number of stock prices. This is because we are traversing the array at most once.
19-
Here, we don't use any additional datastructure other than two pointers. Hence, the space complexity will be O(1).
29+
This algorithm has a time complexity of `O(n)`, where ``n is the number of stock prices. This is because we are traversing the array at most once.
30+
Here, we don't use any additional datastructure other than two pointers. Hence, the space complexity will be `O(1)`.

src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
**Description:**
2+
13
**Algorithmic Steps**
24
This problem is solved with the help of in-place input array using its index as hash key. This is known as **in-place hashing** technique. The algorithmic approach can be summarized as follows:
35

src/javascript/algorithms/array/buySellStock/buySellStock.js renamed to src/javascript/algorithms/array/15.buySellStock/buySellStock.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ function buySellStock(stockPrices) {
22
if(stockPrices.length < 2) return 0;
33

44
let maxProfit = left = 0;
5+
let right = 1;
56

6-
for (let right = 0; right < stockPrices.length; right++) {
7+
while(right < stockPrices.length) {
78
if(stockPrices[right] > stockPrices[left]) {
89
maxProfit = Math.max(maxProfit, stockPrices[right] - stockPrices[left]);
910
} else {
1011
left = right;
1112
}
13+
right++;
1214
}
1315

1416
return maxProfit;
@@ -17,6 +19,6 @@ function buySellStock(stockPrices) {
1719
let stockPrices1 = [8, 3, 6, 4, 7, 5];
1820
let stockPrices2 = [7, 6, 5, 4, 3, 2, 1];
1921

20-
console.log(maxProfit(stockPrices1));
21-
console.log(maxProfit(stockPrices2));
22+
console.log(buySellStock(stockPrices1));
23+
console.log(buySellStock(stockPrices2));
2224

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
**Description:**
2+
Given an array `prices` where `prices[i]` is the price of a given stock on the `i`th day. The profit can be maximized 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, just return 0.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: prices = [8, 3, 6, 4, 7, 5]
8+
Output: 4
9+
10+
Input: prices = [7, 6, 5, 4, 3, 2, 1]
11+
Output: 0
12+
13+
**Algorithmic Steps**
14+
This problem is solved with the help of sliding window approach to calculate the maximum profit(as known as **greedy algorithm**). The algorithmic approach can be summarized as follows:
15+
16+
1. Write a preliminary case by returning profit as `0` if the number of stock prices in the array are less than 2.
17+
18+
2. Initialize `maxProfit` variable to `0` to indicate maximum profit achieved for buying and selling the stock at different days.
19+
20+
3. Initialize left pointer(i.e,`left`) to `0`, which denotes the buying stock index.
21+
22+
4. Iterate over the input stock prices and compare the current stock price(i.e, `stockPrices[right]`) with previous day stock price(i.e, `stockPrices[left]`). If the current stock price is higher, calculate the maximum profit between previous maxProfit and current profit.
23+
24+
5. If the current stock price is lower than previous stock price, consider the current stock price as buying stock with the help of their index pointers(i.e, `left = right`).
25+
26+
6. Return `maxProfit` which represents best time(or max proft scenario) to buy and sell stock prices.
27+
28+
**Time and Space complexity:**
29+
This algorithm has a time complexity of `O(n)`, where ``n is the number of stock prices. This is because we are traversing the array at most once.
30+
Here, we don't use any additional datastructure other than two pointers. Hence, the space complexity will be `O(1)`.

src/javascript/algorithms/array/buySellStock/buySellStock.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)