Naga Vara Pradeep, Yendluri

Home | LinkedIn | GitHub

Best Time to Buy and Sell Stock

Java Implementation

		int maxProfit(int[] prices){
		// TWO POINTER APPROACH
			int i=0, j=1, max = 0;
			while(j < prices.length){
	    			if(prices[i] > prices[j]){
	    				i=j++;
	    			}
	    			else{
	    				max = Math.max(max, prices[j]-prices[i]);
	    				j++;
	    			}
			}
	    	return max;
	    	}