buy and sell

1.只能买卖一次,dp[i+1] = max{dp[i], prices[i+1] - minprices}

/*

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.
*/
class Solution {
public:
//保存前列数据的最小值,在最小值得后面查找可能与其最大差的数据
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if(len <= 1)//长度小于等于1,可能0可能只有一个数据
            return 0;
        int result = prices[1] - prices[0];
        int minprice = prices[0];
        for(int i = 1; i < len - 1; ++ i )
        {
            minprice = min(minprice, prices[i]);
            if(result < prices[i+1] - minprice)
                result = prices[i+1] - minprice;
        }
        if(result < 0)//很可能是【2,1】,特殊情况的处理
             return 0;
        else
            return result;
    }
};
2.一次买一次卖可以进行多次,最大收益

//找到递增递减区间,求差值,麻烦
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int len = prices.size();
        if(len <= 1)return 0;
        int i = 0;
        int res = 0;
        while(i < len - 1)
        {
            int buy, sell;
        
            while(i+1 < len && prices[i+1] < prices[i])i++;//递减区间
            buy = i++;//buy是递减的最小值
         
            while(i < len && prices[i] >= prices[i-1])i++;//递增区间
            sell = i-1;//sell是递增的最大值
            res += prices[sell] - prices[buy];
        }
        return res;
    }
};
同上一题构建股票差价数组,把数组中所有差价为正的值加起来就是最大利润了。其实这和算法1差不多,因为只有递增区间内的差价是正数,并且同一递增区间内所有差价之和 = 区间最大价格 -  区间最小价格


//利用区间内所有差价之和
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int len = prices.size();
        if(len <= 1)return 0;
       
        int res = 0;
        for(int i = 0; i < len - 1 ;++ i)
        {
            if(prices[i] < prices[i+1])
                res += prices[i+1] - prices[i];
        }
        return res;
    }
};



3.只能交易两次,就是在数组中找两队最小最大值,前一个区间和后一个区间[0,1,....,i]  [i+1,.....,n];

dp[i-1] = max{dp[i], maxprices - prices[i-1]}  ,maxprices是区间[i,i+1,...,n-1]内的最高价格

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if(len <= 1)
            return 0;
        int maxprofit = 0;
        int minprice = prices[0];
        int maxFromHead[len];//用一个数组保存从头到i的最大差值
        maxFromHead[0] = 0;
        for(int i = 1; i < len; ++ i)//错在i的值不是i<len-1
        {
            minprice = min(prices[i], minprice);
            if(prices[i] - minprice > maxprofit)
                maxprofit = prices[i] - minprice;
            maxFromHead[i] = maxprofit;
        }
        maxprofit = 0;
        int result = maxFromHead[len - 1];
        int maxprice = prices[len - 1];
        for(int j = len - 2; j >= 0; -- j)//错在j的值,不是j>1,
        {//从后往前计算最大差值,并且与j位置的前序最大差值相加得到result
            maxprice = max(maxprice, prices[j]);
            if( maxprice - prices[j] > maxprofit)
                maxprofit = maxprice - prices[j];
            if(maxprofit + maxFromHead[j] > result)
                result = maxprofit + maxFromHead[j];
        }
        return result;
    }
};

http://www.cnblogs.com/TenosDoIt/p/3436457.html


内容概要:本文全面解析了数智化毕业设计项目开发与写作技巧,涵盖关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势。首先定义了数智化毕业设计项目,强调数据赋能性、智能交互性和场景适配性,并指出数智化写作技巧的重要性。接着介绍了项目开发的“需求锚定 - 技术匹配 - 迭代优化”三步法,以及写作的“问题导向 - 方案论证 - 成果验证”结构。文章列举了教育、医疗、工业等领域的应用场景,如智能学习推荐系统、疾病风险预测模型等。最后通过“基于用户行为数据的智能商品推荐系统”的代码案例,详细展示了数据预处理、协同过滤模型构建及模型评估过程。展望未来,数智化毕业设计将呈现轻量化开发、跨学科融合和落地性强化的趋势。 适合人群:高等院校即将进行毕业设计的学生,特别是对数智化技术感兴趣的理工科学生。 使用场景及目标:①帮助学生理解数智化毕业设计的关键概念和技术实现路径;②指导学生掌握项目开发和写作的具体技巧;③提供实际应用场景和代码案例,增强学生的实践能力;④引导学生关注数智化技术的未来发展趋势。 阅读建议:本文内容丰富,建议读者先通读全文,把握整体框架,再深入研读感兴趣的部分。对于代码案例部分,建议结合实际操作进行学习,加深理解。同时,关注文中提到的未来发展趋势,为自己的毕业设计选题提供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值