# Why the profit would not be used to buy stock next time?

• Judging from my code that has been accepted by the system, I think the limitation of the question is you can only hold 1 stock at most for any time. Which has not been clarified in the question description.

For more complete question and answer to it, in fact case, the money in your account at the beginning is determined. What you can do is to make maximum profit from this data. During the progress, you can use the profit you made to buy more stock next time.

• The condition that you can only hold at most 1 stock for any time is implied in the problem description:

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

If you are already holding a stock, buying another stock is considered two transactions.

• Does this question suppose we must buy stock first? Like for [3,2,1], the answer should be 0?

• /*
january 1st 2015

prices : 17, 15 ,14 ,16 ,19 ,22,24,
-17 . . . +19 . .
. . -14 . . . +24

``````     14 ,15  ... 24 . 25 . . 15 ... 28
+10                    13

14 ,12  ... 24 . 25 . . 15 ... 28
+10                    13

12 13 27 29
+1

1 0 0 -1 000 1 -1 000 1 -1
``````

the idea is to divide the prices array into subarrays where we buy in the lowest price and sell at the highes price.
so whenever that will be profit we should make but keeping in mind that we will not loose another bigger profit after it.
// we will define 0 to not do anything, 1 is to buy and -1 is to sell

*/

public class Solution
{
public int maxProfit(int[] prices)
{
if (prices.length==0 || prices.length==1)
return 0;

``````    int profit=0;
int len=prices.length;
Boolean isHolding=false;

// we will define 0 to not do anything, 1 is to buy and -1 is to sell
int[] pricesOperation=new int[len];
//initialize all to zeros
for(int i=0;i<len;i++)
{
pricesOperation[i]=0;
}

int pr1=prices[0];

for (int j=1;j<len;j++)
{
int pr2 = prices[j];
//int pr3 = prices[j+1];

// if the price is increasing and we do not hold auctions
if(pr1<pr2 && isHolding==false)
{
pricesOperation[j]=-1;
pricesOperation[j-1]=1;
pr1=pr2;
isHolding=true;
}
// if the price is increasing and we hold auctions
else if(pr1<pr2 && isHolding==true)
{
pricesOperation[j]=-1;
if(pricesOperation[j-1]==-1)
{
pricesOperation[j-1]=0;
}
pr1=pr2;
}

// if the price is decreasing and we hold auctions
else if(pr1>pr2 && isHolding==true)
{
pricesOperation[j]=0;
isHolding=false;
pr1=pr2;

}

// if the price is decreasing and we do not hold auctions
else if(pr1>pr2 && isHolding==false)
{
pricesOperation[j-1]=0;
pricesOperation[j]=0;
pr1=pr2;
//isHolding=true;
}
else if(pr1==pr2)
{
pricesOperation[j]=pricesOperation[j-1];
pricesOperation[j-1]=0;
pr1=pr2;

}
pr1=pr2;
}

int pr=0;
for(int k=0;k<len;k++)
{
pr=-pricesOperation[k]*prices[k]+pr;
}
return pr;

}
}``````

• In the real world one transaction almost always has multiples of a single stock, which may trip up people.

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