题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
//动态规划思路
public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int maxProfit = 0; int start = 0; int end = 1; while(end < prices.length){ //如果end位置的值-start位置的值小于0,start = end,否则将end处卖出的值与maxProfit比较 if(prices[end] - prices[start] < 0){ start = end; } maxProfit = prices[end] - prices[start] > maxProfit? prices[end] - prices[start]: maxProfit; end++; } return maxProfit; } }