Your logic is a bit different from others and I personally found it easier.

The purpose of temp and nonn is to keep a track of previous day's buy and sell value. Correct me, if I am wrong about it.

Here, is the JAVA implementation of your logic.

public static int maxProfit(int[] prices) {
if (prices.length<=1) return 0;
int have = prices[1] - prices[0];
int nonn =0, non =0, temp;
for(int i =2;i<prices.length;i++){
temp = have;
have = Math.max(have, nonn) + prices[i]-prices[i-1];
nonn = non;
non = Math.max(have, non);
}
return Math.max(have, non);
}