题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
import java.util.*;
public class Solution {
public int maxProfit (int[] prices) {
// write code here
if(prices==null||prices.length==0||prices.length==1)
return 0;
int max=0;
int[] dp = new int[prices.length];
for(int i=1;i<prices.length;++i){
dp[i] = Math.max(dp[i-1]+prices[i]-prices[i-1],0);
max = Math.max(dp[i],max);
}
return max;
}
}
阿勇算法解集 文章被收录于专栏
对一些基础的,经典的题目的算法题解,每道题的题解尽量做到一题多解,举一反三。其中每一个题解中,若是参考了其他牛人的想法,我会备注出来。
CVTE公司福利 707人发布