【贪心算法】专题练习二

欢迎来到Cefler的博客😁

🕌博客主页:折纸花满衣

🏠个人专栏:题目解析

🌎推荐文章:【LeetCode】winter vacation training


目录

👉🏻买卖股票的最佳时机

原题链接: 买卖股票的最佳时机

mycode:

cpp 复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        //贪心思路:低买高卖
        int min = INT_MAX,max = INT_MIN;
        int ret = 0;
        for(int i = 0;i<prices.size();i++)
        {
            if(prices[i]<min) 
            {
                min = prices[i];
                max = min;//卖出的时间必须大于等于买入时间
                
            }
            if(prices[i]>max) max = prices[i];
            
            if(max-min>ret) ret = max-min;//ret为最大利润
        }
        return ret;
    }
};
相关推荐
cooldream20097 分钟前
计算机专业本科毕业设计系列07——类型④:算法与理论研究
算法·毕业设计·计算机专业·课程设计
劲夫学编程25 分钟前
leetcode: 字母异位词分组
算法·leetcode
wy_hhxx1 小时前
[Leetcode] 动态规划(打家劫舍、0-1背包、完全背包)
算法·leetcode·动态规划·完全背包·0-1背包
m0_547486661 小时前
《智能计算》全套课件PDF(武汉工程大学)
算法·贝叶斯算法·智能计算
weixin_307779132 小时前
Jupyter SQL 表血缘分析并导出 Excel
开发语言·python·sql·算法·excel
Lazyafei2 小时前
560. 和为 K 的子数组
数据结构·算法
小白的高手之路2 小时前
【LeetCode 热题 100】240. 搜索二维矩阵 II|矩阵、二分查找、Z 字形查找(Python)
python·算法·leetcode·矩阵·力扣
geovindu2 小时前
python:DBSCAN
开发语言·后端·python·算法
Tim_102 小时前
【LeetCode】 525. 连续数组
算法
小蒋学算法3 小时前
算法-将数据流变为多个不相交区间-TreeMap
算法