Find Maximum Profit (Directi Question)

2 views
Skip to first unread message

Saurabh Aggarwal

unread,
Sep 9, 2009, 2:28:16 AM9/9/09
to ds--al...@googlegroups.com
The price of a particular stock on different days is given in an array.
e.g. if array is: 1, 8, 2, 10
Price of stock on Day 1 = 1
Price of stock on Day 2 = 8 & so on ..

On any day, you may buy 1 stock, sell 1 stock or do nothing.

What is the maximum profit that you can earn.

(In above example, you buy stock on Day 1, sell it on Day 2, buy on Day 3, sell on Day 4, so maximum profit = 15)


--
Regards,
Saurabh Aggarwal

gaurav gupta

unread,
Sep 9, 2009, 2:54:20 AM9/9/09
to ds--al...@googlegroups.com
I think its more like -1, 0, 1 knapsack problem

-1 == sell
0 == do nothing
1 == buy

and then use DP to solve it.

Suggest if anyone having any better solution.
--
GAURAV GUPTA
B.Tech IV Yr. , Department of Computer Science & Engineering
IT BHU , Varanasi
Contacts
Phone No: +91-99569-49491

e-mail :
gaurav...@acm.org
gaurav.gu...@itbhu.ac.in
Reply all
Reply to author
Forward
0 new messages