Task10

本文链接:
https://blog.csdn.net/qq_34811382/article/details/112972748

121 买卖股票的最佳时机

在这里插入图片描述
思路1:

暴力法 会TLE
双层循环,第一层记录买入,第二层记录卖出,求出最大利润。

代码:

class Solution
{
    
    
public:
    int maxProfit(vector<int> &prices)
    {
    
    
        int BuyIn = 0, SellOut = 0;
        int Profit = 0;
        if(prices.empty())
            return 0; 
        for (int i = 0; i < prices.size(); i++)
        {
    
    
            for (int j = i; j < prices.size(); j++)
            {
    
    
                if (prices[j] - prices[i] > Profit)
                    Profit = prices[j] - prices[i];
            }
        }
        return Profit;
    }
};

思路2:

双指针,利用利用Buy和Sell两个指针遍历数组,找到最大的利润。

代码:

class Solution
{
    
    
public:
    int maxProfit(vector<int> &prices)
    {
    
    
        int Buy = 0, Sell = 1;
        int profit = 0;
        while (Buy < prices.size() && Sell < prices.size())
        {
    
    
            if (prices[Buy] > prices[Sell]) //如果buy指针处的值大于sell指针处的值得话,就说明有更低的买入价
            {
    
    
                Buy = Sell; //令买入位置移动到更低的位置
                Sell++;     //卖出位置后移一位
            }
            else
            {
    
    
                if (prices[Sell] - prices[Buy] > profit)
                    profit = prices[Sell] - prices[Buy];
                Sell++;
            }
        }
        return profit;
    }
};

思路3:

一次遍历,官方解法。我认为是求出一个极小值,再逐步找到极小值与后面的最大利润。

代码:

class Solution
{
    
    
public:
    int maxProfit(vector<int> &prices)
    {
    
    
        int min = prices[0];
        int profit = INT_MIN;
        for (int i : prices)
        {
    
    
            if (min > prices[i])
            {
    
    
                min = prices[i];
            }
            else
            {
    
    
                profit = (profit > (prices[i] - min) ? profit : (prices[i] - min));
            }
        }
        return profit;
    }
};

122 买卖股票的最佳时机 II

在这里插入图片描述
思路:

贪心算法,只比较与下一步的得失,不用长远考虑。即逢低就买入,逢高就卖出。

代码:

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

124 二叉树中的最大路径和

在这里插入图片描述
思路:

递归
最大路径和只有四种情况:1、自己 2、自己加左节点 3、自己加右节点 4、自己加左节点加右节点

介绍一个函数minmax() 需要包含的头文件是algorithm
minmax({v1,v2,v3…}).first 返回{v1,v2,v3…}中的最小值
minmax({v1,v2,v3…}).second 返回{v1,v2,v3…}中的最大值


代码:

class Solution
{
    
    
public:
    int maxval = 0;
    int Findmax(TreeNode *root)
    {
    
    
        if (root = nullptr)
            return 0;
        int l = Findmax(root->left);
        int r = Findmax(root->right);
        int slr = l + r + root->val;
        int LorR = minmax({
    
    l, r, 0}).second + root->val;
        maxval = minmax({
    
    LorR, maxval, slr, root->val}).second;
        return LorR;
    }
    int maxPathSum(TreeNode *root)
    {
    
    
        Findmax(root);
        return maxval;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_34811382/article/details/112972748