算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)

来源:互联网 发布:一般淘宝哪些假货多 编辑:程序博客网 时间:2024/05/17 22:31

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/

题目求的是最大利润,只需要遍历一遍,将budget与profit分别对应记下便好。



class Solution {public:    int maxProfit(vector<int>& prices) {                if(prices.empty())            return 0;                int profit = 0;        int budget = prices[0];                for(int i = 0;i < prices.size();++i)        {            if(budget > prices[i])            {                budget = prices[i];            }            else            {                if(profit < prices[i] - budget)                    profit = prices[i] - budget;            }                    }                return profit;            }};


阅读全文
0 0
原创粉丝点击