LeetCode 188. Best Time to Buy and Sell Stock IV

Solution1:我的答案
参考链接:http://www.cnblogs.com/grandyang/p/4295761.html
当k >= prices.size()时,这道题是II,否则是III

class Solution {
public:int maxProfit(int k, vector<int>& prices) {if (prices.empty()) return 0;if (k >= prices.size()) return other_max(prices);int n = prices.size(), g[n][k+1] = {0}, l[n][k+1] = {0};memset(g, 0, sizeof(g));memset(l, 0, sizeof(l));for (int i = 1; i < prices.size(); ++i) {int diff = prices[i] - prices[i - 1];for (int j = 1; j <= k; ++j) {l[i][j] = max(g[i - 1][j - 1] + max(diff, 0), l[i - 1][j] + diff);g[i][j] = max(l[i][j], g[i - 1][j]);}}return g[n - 1][k];}
private:int other_max(vector<int>& prices) {int n = prices.size();int dp[n] = {0}, res = 0;for (int i = 1; i < n; i++) {dp[i] = max(0, prices[i] - prices[i-1]);res += dp[i];}return res;}
};

【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV相关推荐

  1. LeetCode 188. Best Time to Buy and Sell Stock IV(股票买卖)

    原题网址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ Say you have an array for whi ...

  2. leetcode解题报告:188 Best Time to Buy and Sell Stock IV

    问题: 给定一个列表,第i个元素代表股票第i天的价值,最多只允许买入卖出k次,求最大收益 思路:动态规划 输入为列表p1p2...pm 代码:Python 转载于:https://blog.51cto ...

  3. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

  4. 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

  5. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

  6. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  7. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  8. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  9. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

最新文章

  1. Ubuntu 64位系统安装flash player
  2. docker安装RabbitMQ镜像
  3. Android笔记(二十五) ListView的缓存机制与BaseAdapter
  4. 开源工具 @sap/ux-ui5-tooling 介绍
  5. java ftp上传超时_java - Java FTP连接超时 - 堆栈内存溢出
  6. 04.Python基础_列表_元组_字典_集合
  7. 【深度学习】——pytorch搭建模型及相关模型
  8. Java通过JDBC操作Hive
  9. python 和C语言 中的一些容易混淆的符号整理
  10. 线程安全的量化时间帧环形缓冲区
  11. java h5服务器推送事件_关于H5+中push推送的服务端代码是怎么写的
  12. 机器学习——层次聚类(超详细)
  13. [收藏]Linux 常用命令
  14. 使用jq工具在Shell命令行处理JSON数据
  15. Leet Code OJ 刷题顺序参考
  16. robo3T-操作MongoDB数据库常用命令
  17. heu oj 1011 square
  18. Qt 学习(三) —— Qt 模块
  19. 2022全网最全的爆款抓包工具,各有千秋
  20. Juniper SRX NAT46/NAT64配置

热门文章

  1. 实用js小汇总--获取服务器控件ID
  2. 数据结构笔记(三)-- 链式实现顺序表
  3. linux定时重启tomcat的脚本,Linux系统中的tomcat定时重启脚本
  4. 怎么在大数据里面删除不了_数据库删除大数据怎么操作
  5. mysql数据库的操作dao vo_nutzdao+druid使用非数据库实体VO接收返回的对象一直报“Table ‘xxxxx’ doesn't exist”...
  6. idea创建包怎么让包分层_IDEA让包分层显示的实现方式
  7. Ubuntu18.04安装OpenCV4.3.0和环境配置(支持编译CUDA并安装配置python-opencv)
  8. java 定时程序扫描表_Java扫描程序类
  9. sql选择题_SQL选择
  10. CloudEra Hadoop VMWare单节点环境设置