动态规划实战13 leetcode-121. Best Time to Buy and Sell Stock
前言:这道题目实可以用 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相关推荐
- 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock
LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...
- 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 ...
- 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 ...
- 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 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 ...
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...
- [LeetCode]122. Best Time to Buy and Sell Stock II
[LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...
- LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...
- 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/ 题解 经典的 暴力递归 - ...
- 【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 ...
最新文章
- 英特尔将Nervana技术融入产品路线图
- createprocess失败代码2_极客战记[森林]:边地之叉-通关代码及讲解
- jQuery.sap.factory
- Linux 从源码编译安装 OpenSSL
- POI 导出文件以文件流形式返回
- C++中的也能使用正则表达式
- java arraylist 合并_在Java中将两个arrayList合并到一个新的arrayList中,没有重复且没有顺序...
- 【Http】加解密 生成 X.509格式,DER编码,后缀名.cer 加密公钥证书
- HTML打开网页拒绝访问,WindowsApps无法访问怎么解决?
- jpa 多字段like_Spring Data JPA 如何进行模糊查询(LIKE) ?
- 【工具使用系列】关于 MATLAB Embedded Coder, 你需要知道的事
- Apizza在线接口工具动态绑定API参数依赖
- 80286微处理器和80386的优缺点
- 【K8S】K8s部署Metrics-Server服务
- css竖向箭头符号_「右箭头符号」html实现右箭头 - seo实验室
- 【原创】objdump 的使用
- The Preliminary Contest for ICPC Asia Nanjing 2019
- uni-app开发桌面应用
- 天猫618,吃货们最爱哪些生鲜品牌?
- 四川大学计算机专业调剂,四川大学计算机学院(软件学院)研究生调剂
热门文章
- 超级有用的git reset --hard和git revert命令
- linux mysql远程连接
- HDU 1325 Is It A Tree?(并查集)
- C语言中结构体赋值问题的讨论
- spring 事务源码(三)如何保证被@Transactional标记方法中的所有sql都在一个事务内
- 5脚12v继电器接线图解_继电器线圈并联二极管的作用
- 利用设计模式优化项目实际的申报业务
- Spring中@Autowired注解的工作原理
- MyBatis源码阅读(六) ---mapper方法具体执行流程分析
- springmvc学习一初始化源码