前言:这道题目实可以用 Kadane's Algorithm解决

对于kadane's algorithm 我们可以这么想,假设输入是

[7,1,5,3,6,4]

那么我们可以通过这个数组得到在每一天前买 后一天卖的数组(称收益数组)

[-6,4,-2,3,-2]

实际上这样就转换成了什么问题?没错!maximum subarray!我们只要求收益数组的maximum subarray!

subarray相当于是持有股票的时间段,比如最大的maximum subarray是 [4,-2,3],说明我们要在这段时期持有股票

代码

class Solution {public int maxProfit(int[] prices) {//corner caseif(prices==null||prices.length==0||prices.length==1)return 0;int [] nums = new int[prices.length-1];for(int i=1;i<prices.length;i++){nums[i-1]=prices[i]-prices[i-1];}int result= maxSubArray(nums);return result<0 ? 0:result;}public int maxSubArray(int[] nums) {int n = nums.length;int[] dp = new int[n];//dp[i] means the maximum subarray ending with A[i];dp[0] = nums[0];int max = dp[0];for(int i = 1; i < n; i++){dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);max = Math.max(max, dp[i]);}return max;}
}

实际上我们可以将代码简化,可以上leetcode相应solution去寻找。

不过个人感觉这样写的代码更容易让人理解

动态规划实战13 leetcode-121. Best Time to Buy and Sell Stock相关推荐

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

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

  2. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

  3. 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 w ...

  4. 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 ...

  5. 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 w ...

  6. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

  7. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

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

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

  9. 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/ 题解 经典的 暴力递归 - ...

  10. 【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 ...

最新文章

  1. 英特尔将Nervana技术融入产品路线图
  2. createprocess失败代码2_极客战记[森林]:边地之叉-通关代码及讲解
  3. jQuery.sap.factory
  4. Linux 从源码编译安装 OpenSSL
  5. POI 导出文件以文件流形式返回
  6. C++中的也能使用正则表达式
  7. java arraylist 合并_在Java中将两个arrayList合并到一个新的arrayList中,没有重复且没有顺序...
  8. 【Http】加解密 生成 X.509格式,DER编码,后缀名.cer 加密公钥证书
  9. HTML打开网页拒绝访问,WindowsApps无法访问怎么解决?
  10. jpa 多字段like_Spring Data JPA 如何进行模糊查询(LIKE) ?
  11. 【工具使用系列】关于 MATLAB Embedded Coder, 你需要知道的事
  12. Apizza在线接口工具动态绑定API参数依赖
  13. 80286微处理器和80386的优缺点
  14. 【K8S】K8s部署Metrics-Server服务
  15. css竖向箭头符号_「右箭头符号」html实现右箭头 - seo实验室
  16. 【原创】objdump 的使用
  17. The Preliminary Contest for ICPC Asia Nanjing 2019
  18. uni-app开发桌面应用
  19. 天猫618,吃货们最爱哪些生鲜品牌?
  20. 四川大学计算机专业调剂,四川大学计算机学院(软件学院)研究生调剂

热门文章

  1. 超级有用的git reset --hard和git revert命令
  2. linux mysql远程连接
  3. HDU 1325 Is It A Tree?(并查集)
  4. C语言中结构体赋值问题的讨论
  5. spring 事务源码(三)如何保证被@Transactional标记方法中的所有sql都在一个事务内
  6. 5脚12v继电器接线图解_继电器线圈并联二极管的作用
  7. 利用设计模式优化项目实际的申报业务
  8. Spring中@Autowired注解的工作原理
  9. MyBatis源码阅读(六) ---mapper方法具体执行流程分析
  10. springmvc学习一初始化源码