题解 | #买卖股票的最好时机(四)#

买卖股票的最好时机(四)

http://www.nowcoder.com/practice/1c583d416d504b80821fbe4cc20404f3



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param prices int整型一维数组 
     * @param k int整型 
     * @return int整型
     */
    public int maxProfit (int[] prices, int k) {
        if (prices.length <= 1 || k == 0) {
			return 0;
		}
		int len = prices.length;
		int[] buy = new int[len + 1];
		int[] sell = new int[len + 1];
		Arrays.fill(buy, -prices[0]);
		Arrays.fill(sell, 0);
		for (int p : prices) {
			for (int i = 1; i <= k; i++) {
				buy[i] = Math.max(buy[i], sell[i - 1] - p);
				sell[i] = Math.max(sell[i], buy[i] + p);
			}
		}
		return sell[k];
    }
}
全部评论

相关推荐

简历求拷打,海投简历发过去就已读不回了求大佬们指点
程序员牛肉:基本不能了,估计你得放弃秋招,九月份找实习之后明年的春招开始正式找工作
点赞 评论 收藏
分享
陈逸轩1205:才105 哥们在养生呢
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务