B. Wrong Solution Score: 1 CPU: 1s Memory: 1200MB Given a list of stock values, where the i-th element is the value of the stock on the i-th day. Find the maximum money you can earn by buying and selling stocks, given that you can buy or sell (not both) only one stock on a particular day. However, you can have multiple stocks with you at any point of time. You have to buy a stock before you can sell it. Example: stock_prices[] = {2,4,6,8,10},Optimal solution would be {(1,5), (2,4)} (1-based indexing), which means : buy a stock on day 1 and sell that on day 5. buy a stock on day 2, and sell it on day 4. This way you can earn: 10 + 8 – 2 - 4 = 12. For this problem, someone has submitted the following code. The idea of the code is given briefly (initially all the stocks are unmarked): Find the day X where the stock value on day X is the maximum among all the days not yet marked. Choose the rightmost one in case of tie. Find the day Y where Y is before X (Y < X) a...
Comments
Post a Comment