SarthakKeshari / Java-Questions-and-Solutions

This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
MIT License
47 stars 98 forks source link

Created Best Time to Buy and Sell Stock III #294

Closed Onkar627 closed 3 years ago

Onkar627 commented 3 years ago

Issue Id you have worked upon -

Issue #290

Briefly explain your program logic -

We can solve it O(n^2) using that technique also. Like if we partition the array into two groups [i.....j] and [j+1......n] and Now for each j=1 to j=n-1 we can call find the maximum profit which will be the addition of the profits of these groups and at the last return the maximum.

We can also solve it in O(n) time by using DP by storing the maxprofit at each index from left and from right as well. Thus, we don't need to compute it again and again. The following is the code for this approach.

Screenshots(Attach 2 screenshots of your own input and output) -

Attach here ![Uploading Screenshot 2021-10-07 at 3.33.42 PM.png…]()

Screenshot 2021-10-07 at 3 33 55 PM


Checklist:

Eg - If your code follow the below guidelines. Kindly change [] to [x]

All the conditions should be fulfilled for considering your code for merging -

SarthakKeshari commented 3 years ago

@Onkar627, Kindly fill the checklist in the above PR by agreeing to it.

Onkar627 commented 3 years ago

@SarthakKeshari I have made the changes as requested.Kindly review.