Best Time to Buy and Sell Stock

本文详细探讨了四种不同约束条件下的股票交易最大利润算法。包括仅允许进行一次交易、允许进行多次交易但不能同时持有、允许多次交易但需遵循冷却期规则以及限制交易次数的情况。通过动态规划和贪心算法解决这些问题。

Best Time to Buy and Sell Stock 

Best Time to Buy and Sell Stock 1
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

solution:
dp[i] contains the smallest prices until i-day;
the state transition equation is:
if prices[i] < dp[i - 1] then dp[i] = prices[i];
if prices[i] >= dp[i - 1] then dp[i] = dp[i-1];

int maxProfit(vector<int>& prices) 
{
    int n = prices.size();
    if (n <= 0) return 0;
    int max_profit = 0;
    int *dp = new int[n]();
    //memset(dp,0,sizeof(int)*n);
    for (int i = 0; i < n; ++i)
    {
        if (i == 0 || prices[i] < dp[i - 1])
            dp[i] = prices[i];
        else
        {
            dp[i] = dp[i - 1];
            max_profit = max(max_profit, prices[i] - dp[i]);
        }
    }
    delete[]dp;
    return max_profit;
}

duo to the fact that dp[i] only relies on previous state, we have the simplied form:
int maxProfit(vector<int>& prices) 
{
    int n = prices.size();
    if (n <= 0) return 0;
    int max_profit = 0, smallest_price = 0;
    for (int i = 0; i < n; ++i)
    {
        if (i == 0 || prices[i] < smallest_price)
            smallest_price = prices[i];
        else
            max_profit = max(max_profit, prices[i] - smallest_price);
    }
    return max_profit;
}
 
Best Time to Buy and Sell Stock
 2 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

solution:

reference: https://leetcode.com/discuss/71354/share-my-thinking-process

2 possible transactions: buy and sell 

buy[i] means before day i what is the maxProfit for any sequence end with buy.

sell[i] means before day i what is the maxProfit for any sequence end with sell.

state transition equation is: 

buy[i] = max(sell[i-1]-price, buy[i-1]) 

sell[i] = max(buy[i-1]+price, sell[i-1])

int maxProfit(vector<int> &prices) 
{
    int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;
    for (int price : prices) 
    {
        prev_buy = buy;
        prev_sell = sell;
        buy = max(prev_sell - price, buy);
        sell = max(prev_buy + price, sell);
    }
    return sell;
}
 another solution: greedy algorithm
int maxProfit(vector<int>& prices) 
{
    int max_profit= 0;
    for(int i=1;i<prices.size();++i)
    {
        max_profit += max(0,prices[i]-prices[i-1]);
    }
    return max_profit;
}
 

Best Time to Buy and Sell Stock 3 

Say you have an array for which the ith element is the price of a given stock on day i. 

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:   You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).   After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) 

solution:

reference: https://leetcode.com/discuss/71354/share-my-thinking-process 

state transition equation is: 

buy[i] = max(sell[i-2]-price, buy[i-1]) 

sell[i] = max(buy[i-1]+price, sell[i-1])

int maxProfit(vector<int> &prices) 
{
    int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;
    for (int price : prices) 
    {
        prev_buy = buy;
        buy = max(prev_sell - price, buy);
        prev_sell = sell;
        sell = max(prev_buy + price, sell);
    }
    return sell;
}
 

Best Time to Buy and Sell Stock 4 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most K transactions.

solution:

f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions.     

f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] }             

  = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))   

f[0, ii] = 0; 0 times transation makes 0 profit

f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade 

int maxProfit(vector<int> &prices,int K) 
{
    if (prices.size() <= 1) return 0;
    int maxProf = 0;
    vector<vector<int>> f(K+1, vector<int>(prices.size(), 0));
    for (int kk = 1; kk <= K; kk++) 
    {
        int tmpMax = f[kk-1][0] - prices[0];
        for (int ii = 1; ii < prices.size(); ii++) 
        {
            f[kk][ii] = max(f[kk][ii-1], prices[ii] + tmpMax);
            tmpMax = max(tmpMax, f[kk-1][ii] - prices[ii]);
            maxProf = max(f[kk][ii], maxProf);
        }
    }
    return maxProf;
}


基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值