LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
这三道题,很好的进阶。1题简单处理,2题使用贪心,3题使用动态规划。
话说这叫一维动态规划,嗯。又复习了《算法导论》中和贪心以及动态规划有关的知识,记录如下:
动态规划的标志:最优子结构、子问题重叠。
1.找最优子结构
2.定义递归公示(列一个式子出来,并定义好这个式子到底是什么意思)。
3.写自底向上或递归备忘录法。
比如本问题:f(i,j) = max{f(i,k)+f(k,j)} 其中:f(i,j)表示从i到j的所有数,进行一次交易能获得的最多的钱数。最终要求的是f(0,n-1),k从1到n-2.
再深化一下:
fp(i,j)表示从i 到 j 进行最多进行 p 次交易可以获得的钱数,则:
fp(i,j) = max {fp-1(i,j), fp-p'(i,k)+fp'(k,j)} 其中:p'从1到p-1,k从i+1到j-1.
这大概叫二维动态规划(或许)。
贪心算法是从局部最优,能够做到全局最优。可分成一步步的,当前的选择受之前做过的选择的影响。而动态规划,是受后面要做的选择的影响。
于是有了下面代码,复杂度O(n2)然后超时了。
class Solution {
public:
//start 第一个元素,end 最后一个元素的下标
int onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
}
return ans;
}
int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ;
int ans = onceBuyAndSell(prices,,prices.size()-);
if(prices.size()< || ans == )
return ans;
int temp = ;
for(int itor = ;itor<prices.size()-;itor++)
{
temp = onceBuyAndSell(prices,,itor) + onceBuyAndSell(prices,itor+,prices.size()-);
if(temp> ans)
ans = temp;
}
return ans;
}
};
使用了动态规划中打表的方法,降低了复杂度O(n).代码如下:
class Solution {
public:
vector<int> ansN;
//ansN[i]表示,从i到最后的所有元素,只进行一次交易的话,可以得到的最多值。
void onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
ansN[end] = ;
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
ansN[i] = ans;
}
}
int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ;
ansN.resize(prices.size());
//先来一遍从后面计算的。
onceBuyAndSell(prices,,prices.size()-);
if(prices.size()< )
return ansN[];
int min = prices[];
int ans2 = ;
int ans = ansN[];
for(int itor = ;itor<prices.size()-;itor++)
{
if(prices[itor] - min>ans2)
ans2 = prices[itor] - min;
if(prices[itor]<min)
min = prices[itor];
if(ans2 + ansN[itor+]> ans)
ans = ans2 + ansN[itor+];
}
return ans;
}
};
LeetCode OJ--Best Time to Buy and Sell Stock III的更多相关文章
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【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 123. Best Time to Buy and Sell Stock III ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 【 Best Time to Buy and Sell Stock III 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- LeetCode939
问题:最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,1],[1,3], ...
- python入门:BREAK 的用法 跳当前循环后,不再执行下面代码块
#!/urs/bin/env python # -*- coding:utf-8 -*- # BREAK 的作用 跳当前循环后,不再执行下面代码块 while True: ') break ') #w ...
- linux文件属性软硬链接知识
链接的概念 在linux系统中,链接可分为两种:一种为硬链接,另一种为软链接或符号链接.在默认不带参数的情况下,执行ln命令创建的链接是硬链接. 如果使用ln -s创建链接则为软链接,前面文件类型为 ...
- jquery图片切换插件jquery.cycle.js参数详解
转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...
- 【markdown】 markdown 语法
介绍几个 markdown 语法学习地址和相关工具 参考链接 coding gitlab markdown offical markdown editor markdown editor2
- manjaro(arch)里的vbox 安装centos7后,centos无法联网的解决办法
第一步,在VirtualBox中设置网卡连接方式:点“设置”,在弹出的界面中点“网络”,最后选择“连接方式”为“桥接网卡”. 回到centOS中,进入终端,输入命令:ip addr,查看网络配置文件的 ...
- 在ArchLinux、manjaro中安装MySql(mariaDB)
安装MySql数据库.但是在MySql被Oracle收购之后,很多开源支持者就转而使用MariaDb了.不过MariaDb也和MySql兼容的,所以基本不用有什么担心.由于ArchLinux只带了Ma ...
- OpenCV中的图像形态学转换
两个基本的形态学操作是腐蚀和膨胀.他们的变化构成了开运算,闭运算,梯度等.下面以这张图为例 1.腐蚀 这个操作会把前景物体的边界腐蚀掉. import cv2 import numpy as np i ...
- #pragma与_Pragma(转载)
C90为预处理指令家族带来一位新成员:#pragma.一般情况下,大家很少见到它. #pragma的作用是为特定的编译器提供特定的编译指示,这些指示是具体针对某一种(或某一些)编译器的, ...
- C3P0连接问题
C3P0 连接时的相关问题: 我的环境是在IDEA中使用C3P0中进行的: 使用C3P0主要用到的jar包都是最新和Mysql8.0兼容的包 在连接的时候遇到: 先是在连接的时候出现数据库连接的时候的 ...