动态规划——Best Time to Buy and Sell Stock III
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
示例 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
示例 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
int maxProfit(int* prices, int pricesSize) {
int ans = ;
if (pricesSize > ){
int*dp = (int*)malloc(pricesSize*sizeof(int));
dp[] = ;
int ans2 = ;
for (int i = ; i <= pricesSize; i++){
dp[] = ;
ans2 = ;
for (int j = ; j<i; j++){
dp[j] = (prices[j] + dp[j - ] - prices[j - ]) > ? (prices[j] + dp[j - ] - prices[j - ]) : ;
ans2 = ans2 >= dp[j] ? ans2 : dp[j];
}
ans = ans >= ans2 ? ans : ans2;
if (i < pricesSize)dp[i] = ;
for (int k = i+; k<pricesSize; k++){
dp[k] = (prices[k] + dp[k - ] - prices[k - ]) > ? (prices[k] + dp[k - ] - prices[k - ]) : ;
ans = ans >= (dp[k]+ans2) ? ans : (dp[k]+ans2);
}
}
//free(dp);
}
else if (pricesSize == ){
ans = (prices[] - prices[]) > ? (prices[] - prices[]):;
}
else ans = ;
return ans;
}
但是非常搞笑的是我这个代码虽然在LeetCode上成功AC,但是应该是最差的一个解决方案。。。但是思路上非常便于理解

动态规划——Best Time to Buy and Sell Stock III的更多相关文章
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 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 ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【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 ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
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
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- [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 ...
随机推荐
- golang 代码笔记
锁 互斥锁,g0获取锁,到释放锁之间,g1去获取锁失败,阻塞,g0释放锁之后g1获取锁成功,gn阻塞. package main import ( "fmt" "sync ...
- 洛谷P4719 动态dp
动态DP其实挺简单一个东西. 把DP值的定义改成去掉重儿子之后的DP值. 重链上的答案就用线段树/lct维护,维护子段/矩阵都可以.其实本质上差不多... 修改的时候在log个线段树上修改.轻儿子所在 ...
- Aerospike-内存和硬盘混合存储的kv数据库
为什么会有Aerospike? Redis是一个纯内存型数据库,性能上没有多大问题. 但这又带来一个新问题,内存是很贵的,所以全内存的存储成本非常昂贵.为了节省成本,我们需要把一部分不经常用到的数据存 ...
- 2018年秋季学期《c语言程序设计》助教总结
<c语言程序设计>第七周助教总结 <c语言程序设计>第八周助教总结 <c语言程序设计>第九周助教总结 <c语言程序设计>第十周助教总结 <c语言程 ...
- 使用svn进行协作开发
环境 操作系统:win7 64位 所需工具 1. 服务器端(Subversion)[Setup-Subversion-1.8.16.msi] 2. 客户端(TortoiseSVN)[TortoiseS ...
- androidstudio上传代码到git上
1.首先通过git --bare init 在服务端创建好了一个git仓库:假设git仓库在服务端的地址为:/user/lyh/project/test.git 2.androidstudio上点击V ...
- IP地址转为二进制,去掉0b补齐八位拼接,再转为十进制
#!/usr/bin/env python# -*- coding:utf-8 -*- ip = '192.168.0.1' # 转为二进制:# 方法一'''eve = ip.split('.')s ...
- 网络学习day04_VLSM、子网划分
IP子网划分 首先,在进行子网划分的学习之前,我们先来回顾一下IP地址的相关知识,同时了解一下公有和私有IP地址: 在Internet上有千百万台主机,为了区分这些主机,人们给每台主机都分配了一个专门 ...
- DeepLearning.ai学习笔记(五)序列模型 -- week2 序列模型和注意力机制
一.基础模型 假设要翻译下面这句话: "简将要在9月访问中国" 正确的翻译结果应该是: "Jane is visiting China in September" ...
- Ansible之Playbook详解、案例
什么是playbook playbooks是一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的 ...