@ harsha : consider a case like this : 3 11 1 7 by your algo 1 is the minimum stock price, and you will sell it for 7-1=6 whereas the max prize is 11-3=8,
I have code like this : double maxProfit = 0, minBuy = 10000000; int minBuyTime = -1, resultBuyTime = -1, resultSellTime = -1; for (int i=0; i<N; i++) { //consider selling now double saleProfit = arr[i] - minBuy; if (saleProfit > maxProfit) { maxProfit = saleProfit; resultBuyTime = minBuyTime; resultSellTime = arr[i]; } //consider if there's a new minimum buy price if (arr[i] < minBuy) { minBuy = arr[i]; minBuyTime = i; } } printf ("Buy time: %lf, Sell Time: %lf, Profit: %lf", resultBuyTime, resultSellTime, maxProfit); I hope this would work, also could u elaborate on DeShaw's procedure, was there a separate coding round On Sunday, August 5, 2012 10:07:21 AM UTC+5:30, harsha wrote: > > there is a stock company whose stock prices are known to u for next 365 > days. u have to write the code on which day u should buy and sell the > stock. U cannot sell a stock until you haven't bought any stock. > > my approach was to buy the stock when the stock prices are the lowest and > sell them when they are the highest. But i think am missing something > because this question was for a considerable amount of marks but the > solution is too obvious! any thoughts ? > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/oFONPkTw7JgJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.