[LeetCode] Best Time to Buy and Sell Stock III Solution

本文介绍了一种使用一维动态规划解决股票交易问题的方法,目标是在最多完成两次交易的情况下获取最大利润。通过将整个数组分为两部分并计算每部分的最大值,最终确定最优交易策略。

Say you have an array for which the  i th element is the price of a given stock on day  i.
Design an algorithm to find the maximum profit. You may complete at most  two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

» Solve this problem

[Thoughts]
One dimensional dynamic planning.
Given an i, split the whole array into two parts:
[0,i] and [i+1, n], it generates two max value based on i, Max(0,i) and Max(i+1,n)
So, we can define the transformation function as:
Maxprofix = max(Max(0,i) + Max(i+1, n))  0<=i<n
Pre-processing Max(0,i) might be easy, but I didn’t figure an efficient way to generate Max(i+1,n) in one pass.

[Code]

1:    int maxProfit(vector<int> &prices) {  
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int index =0;
5: int max1, max2;
6: int max =0;
7: for(int i =0; i< prices.size(); i++)
8: {
9: max1=SearchMax(prices,0,i);
10: max2 = SearchMax(prices,i+1, prices.size()-1);
11: if(max < max1+max2)
12: max = max1+max2;
13: }
14: return max;
15: }
16: int SearchMax(vector<int>& prices, int start, int end)
17: {
18: int max=0;
19: int min=INT_MAX;
20: for(int i =start; i<= end; i++)
21: {
22: if(min> prices[i]) min = prices[i];
23: int diff = prices[i] -min;
24: if(diff> max)
25: {
26: max = diff;
27: }
28: }
29: return max;
30: }

Update 03/12/2014
Just see the comments. Looks I didn’t post the right code.
Line 6~12 and Line 15~21 can be merged into one pass. But keep it for readability.

1:  int maxProfit(vector<int> &prices) {   
2: if(prices.size() <= 1) return 0;
3: vector<int> maxFromLeft(prices.size(), 0);
4: vector<int> maxFromRight(prices.size(), 0);
5: int minV = INT_MAX, maxP = INT_MIN;
6: for(int i =0; i< prices.size(); i++)
7: {
8: if(minV > prices[i]) minV = prices[i];
9: int temp = prices[i] - minV;
10: if(temp > maxP) maxP = temp;
11: maxFromLeft[i] = maxP;
12: }
13: int maxV = INT_MIN;
14: maxP = INT_MIN;
15: for(int i =prices.size()-1; i>=0; i--)
16: {
17: if(maxV < prices[i]) maxV = prices[i];
18: int temp = maxV - prices[i];
19: if(temp > maxP) maxP = temp;
20: maxFromRight[i] = maxP;
21: }
22: int maxProfit = INT_MIN;
23: for(int i =0; i< prices.size()-1; i++)
24: {
25: int sum = maxFromLeft[i] + maxFromRight[i+1];
26: if(sum > maxProfit) maxProfit = sum;
27: }
28: if(maxProfit < maxFromRight[0])
29: maxProfit = maxFromRight[0];
30: return maxProfit;
31: }
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合数据驱动方法与Koopman算子理论的递归神经网络(RNN)模型线性化方法,旨在提升纳米定位系统的预测控制精度与动态响应能力。研究通过构建数据驱动的线性化模型,克服了传统非线性系统建模复杂、计算开销大的问题,并在Matlab平台上实现了完整的算法仿真与验证,展示了该方法在高精度定位控制中的有效性与实用性。; 适合人群:具备一定自动化、控制理论或机器学习背景的科研人员与工程技术人员,尤其是从事精密定位、智能控制、非线性系统建模与预测控制相关领域的研究生与研究人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能预测控制;②为复杂非线性系统的数据驱动建模与线性化提供新思路;③结合深度学习与经典控制理论,推动智能控制算法的实际落地。; 阅读建议:建议读者结合Matlab代码实现部分,深入理解Koopman算子与RNN结合的建模范式,重点关注数据预处理、模型训练与控制系统集成等关键环节,并可通过替换实际系统数据进行迁移验证,以掌握该方法的核心思想与工程应用技巧。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值