Skip to content

Commit 79cf9eb

Browse files
committedDec 23, 2020
Add divide and conquer example: best time to buy and sell stocks.
1 parent 4973392 commit 79cf9eb

File tree

1 file changed

+3
-3
lines changed
  • src/algorithms/uncategorized/best-time-to-buy-sell-stocks

1 file changed

+3
-3
lines changed
 

‎src/algorithms/uncategorized/best-time-to-buy-sell-stocks/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ _Explanation:_ In this case, no transaction is done, i.e. max `profit = 0`.
3737

3838
## Possible Solutions
3939

40-
### Divide and conquer approach
40+
### Divide and conquer approach `O(2^n)`
4141

4242
We may try **all** combinations of buying and selling and find out the most profitable one by applying _divide and conquer approach_.
4343

@@ -62,7 +62,7 @@ As you may see, this is very inefficient. For example for just `20` prices the n
6262

6363
If we avoid cloning the prices array between recursive function calls and will use the array pointer then additional space complexity will be proportional to the depth of the recursion: `O(n)`
6464

65-
## Peak Valley Approach
65+
## Peak Valley Approach `O(n)`
6666

6767
If we plot the prices array (i.e. `[7, 1, 5, 3, 6, 4]`) we may notice that the points of interest are the consecutive valleys and peaks
6868

@@ -82,7 +82,7 @@ Since the algorithm requires only one pass through the prices array, the time co
8282

8383
Except of the prices array itself the algorithm consumes the constant amount of memory. Thus, additional space complexity is `O(1)`.
8484

85-
## Accumulator Approach
85+
## Accumulator Approach `O(n)`
8686

8787
There is even simpler approach exists. Let's say we have the prices array which looks like this `[1, 7, 2, 3, 6, 7, 6, 7]`:
8888

0 commit comments

Comments
 (0)
Please sign in to comment.