leetcode 188-maxProfit
public static int maxProfit(int k, int[] prices) {
if (0 >= k || null == prices || 1 >= prices.length)
return 0;
int r = 0, plen = 0;
if (k >= (plen = prices.length) / 2) {
for (int i = 1; i < plen; i++)
if (prices[i] > prices[i - 1])
r += prices[i] - prices[i - 1];
return r;
}
//每次交易的支出金额(负值表示正常支出、正值表示不再支出即收益)
int[] buy = new int[k + 1];
//每次交易赢得的最大收益
int[] sell = new int[k + 1];
//首次交易统一是支出
Arrays.fill(buy, -prices[0]);
for(int i = 0; i < plen; i++) {
for(int j = 1; j <= k; j++) {
//取得累计天次中当次交易可以得到的最小支出金额(上一天的支出额 VS 当天前一次交易的最大收益 - 当天股价)
buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
//取得累计天次中当次交易可以得到的最大收益(上一天的最大收益 VS 支出额 +当天股价)
sell[j] = Math.max(sell[j], buy[j] + prices[i]);
}
}
return sell[k];
}
leetcode 188-maxProfit的更多相关文章
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 188. 买卖股票的最佳时机 IV
参见 本题采用了第一列初始化后,从左侧向右开始递推的方式,但从上往下递推应该也成立,以后尝试一下 想写一个普适性的适用于n天交易k次持有j股的状态方程但是有问题:对于交易次数过多的情况数组会超出界限: ...
- Java实现 LeetCode 188 买卖股票的最佳时机 IV
188. 买卖股票的最佳时机 IV 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意: 你不能同时参与多 ...
- LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Leetcode 188.买卖股票的最佳时机IV
买卖股票的最佳时机IV 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意: 你不能同时参与多笔交易(你必 ...
- LeetCode #188场周赛题解
A题链接 给你一个目标数组 target 和一个整数 n.每次迭代,需要从 list = {1,2,3..., n} 中依序读取一个数字. 请使用下述操作来构建目标数组 target : Push:从 ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- swift2.0学习之拓展
拓展:和oc的拓展方法功能差点儿相同.就是给已经存在的类,结构体.枚举,协议类型添加新的方法 拓展语法: 用extensionkeyword声明: extension SomeType { // ne ...
- Mac关闭Iphone更新系统iTunes强制自动备份文件
在任何时候iOS设备一连结苹果Mac电脑,电脑中的iTunes软件将自动对iOS设备进行同步和备份.虽然备份非常有用,当我们的iPhone/iPad出现问题的时候,可以直接恢复iPhone/iPad的 ...
- Xcode 自己主动生成版本技术最佳实践
在 bloglovin ,我们使用自己主动生成版本来设置Xcode,使当前的版本为在Git活跃的分支上 的提交数. 它一直正常工作着.但我们的技术也不是一帆风顺的. 糟糕的老方法 我们使用的技术是来自 ...
- oc36--自定义构造方法在继承中的表现
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...
- oc31--new实现
// // main.m // new方法实现原理 #import <Foundation/Foundation.h> #import "Person.h" int m ...
- Git Stash方法
命令:git stash1.使用git stash 保存当前的工作现场, 那么就可以切换到其他分支进行工作,或者在当前分支上完成其他紧急的工作,比如修订一个bug测试提交. 2.如果一个使用了一个gi ...
- bzoj 4481 [ Jsoi 2015 ] 非诚勿扰 —— 期望
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4481 太弱了这种题都要看半天TJ...:https://blog.csdn.net/chai ...
- 二维矩阵相乘 in C++
#include <iostream> #include <vector> #include <string> #include <sstream> # ...
- 原生JS---7
原生js学习笔记7——本地存储之cookie操作 什么是cookie • 用来保存页面信息的,如用户名.密码 • cookie的特性:同一个网站中所有的页面共享一套cookie:数量.大小限制:过期时 ...
- selenium3 + python - gird分布式(转载)
本篇转自博客:上海-小T 转载链接:https://blog.csdn.net/real_tino/article/details/53467406 Selenium grid是用来分布式执行测试用例 ...