You thought too much. If you talk about multiple day, say 3 days, and the price is 3,1,5, and you thought about 3 buy, 5 sell. But actually you will make the most if buy at 1 and sell at 5. If the price is 3,9,5, the best would just do 3->9, and forget about the 5. If 3,4,5, you can say 3->5 is the best, but my code will give you 3->4 and 4->5, which is the same.
J
jyan
@jyan
328
Reputation
2
Posts
438
Profile views
2
Followers
0
Following
Posts made by jyan
-
RE: Is this question a joke?
-
Is this question a joke?
public class Solution { public int maxProfit(int[] prices) { int total = 0; for (int i=0; i< prices.length-1; i++) { if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i]; } return total; }
A simple code like this. The designer of this question must thought of something too complicated.