# 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];
}

}
``````

A simple code like this. The designer of this question must thought of something too complicated.

• You are assuming you "gain money" every day, when in fact you only do when you sell a stock you bought at a previous (not necessarily right before) day.

• his code is actually correct.

After the modification from the Stock I, the question is a lot simpler. You just need to buy/sell when it is profitable.

• 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.

• it may not be obvious if one doesn't get the intuition right.
No short allowed, to max you profit, you will just need capture every uptick, that's it.

• No question is a joke. This question is indeed the interview question I encountered interviewing with Goldman Sachs. Interview questions from financial organization my be a lot simpler to solve. But it requires higher level of clarity when it comes to explaining your thought. And most importantly, arrogance is the first thing you want to avoid during interview. Don't murder me if you don't like my comment. LOL

• The algorithm for "max profit" would be "sum of all the peak increments / inclination [i.e. if you plot graph by using prices vs days]" which is exactly what your code does which is short although it doesn't look obvious at first and you're increment is daily if any.

My code:

``````int maxProfit(vector<int>& prices) {
int sell_day = 0;
int max_prof = 0;
int l = prices.size();

while ((buy_day < l - 1) && (sell_day < l - 1) ) {
if (buy_day == l - 1) break;

while ( (sell_day < l - 1) && (prices[sell_day + 1] >= prices[sell_day]) ) sell_day++;
}
return max_prof;
}``````

• This post is deleted!

• However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

I think that note may mean "you can't sell and buy at the same time position".

Take "1 2 3" for example, you should 0+(3-1) instead of 0+(2-1)+(3-2)

It may leave out a lot of add operation.

• I also think of something too complicated at first time. even tried to simulate the transaction choices of each day by backtrack.

• Yes, If this condition i.e. " you can't sell and buy at the same time position " is included, then the problem becomes a lot harder.

• nice code! i must thought this question too complicated

• I think we need some clarification on "you may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again)". If it is required that you cannot sell and buy on the same day, then the code above is not following this rule, even though it will give you the right answer. Just say we have prices given by [1, 2, 3], following this rule, if you buy at price 1 and then sell it at price 2, the profit is 1. But now you cannot buy at price 2 again and sell it at price 3, which will lose 1 profit. The right way to do is buy at 1 and sell at 3, the profit is 3 - 1 = 3 - 2 + 2 - 1 = 2. So if this is the case, you should at least convey this rule in your code. If not, then the code above is indeed a good one.

• I don't think the code has any conflict with the requirement "you can't sell and buy at the same time position". Accumulating the gain profit every day does not have to mean that I sell and buy the stock every day. I just check my account and say :"wow, I gained more xx profit!" when the stock price is increasing.

• the question is kind of dilemma, the key is the understanding of same time. LOL

• This post is deleted!

• really neat!

• This post is deleted!

• I agree with you. I think the question meant to ask a "one-time" buy/sell choice, which is a little bit more complicated than this.

This is just too simple.

• In fact the title of this question is misleading: "The best time to buy and sell".

"Best time" suggests it is a one-time action.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.