[LeetCode] Best Time to Buy and Sell Stock

简介: Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.If you were only permitted to complete at most one transaction (ie, buy one and sell one shar

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.

解题思路

首先设定股票买入价格为prices[0],遍历整个数组,如果当前元素的值小于买入价格,则将该元素值设为买入价格;如果当前元素值大于等于下一元素值(该值为极大值),则将局部最大值设为当前元素值与买入价格的差值,再将局部最大值与全局最大值进行比较,如果,如果大于全局最大值,则将局部最大值赋给全局最大值。最后得到的全局最大值即为所求。

实现代码1

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/17 22:00
    *  @Status   : Accepted
    *  @Runtime  : 17 ms
******************************************************************/
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if (prices.size() <= 1)
        {
            return 0;
        }
        int max_local = 0;
        int max_global = 0;
        int base = prices[0];
        int i = 0;
        while (i < prices.size() - 1)
        {
            if (prices[i] < base)
            {
                base = prices[i];
            }
            else if (prices[i] >= prices[i + 1])
            {
                max_local = prices[i] - base;
                max_global = max_global > max_local ? max_global : max_local;
            }
            i++;
        }
        max_local = prices[i] - base;
        max_global = max_global > max_local ? max_global : max_local;

        return max_global;
    }
};

实现代码2

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if (prices.size() == 0)
        {
            return 0;
        }

        int max_local = 0;
        int max_global = 0;
        for (int i = 0; i < prices.size() - 1; i++)
        {
            max_local = max(max_local + prices[i + 1] - prices[i], 0);
            max_global = max(max_local, max_global);
        }

        return max_global;
    }
};
目录
相关文章
|
算法
LeetCode 309. Best Time to B & S Stock with CD
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票)
71 0
LeetCode 309. Best Time to B & S Stock with CD
|
算法
LeetCode 188. Best Time to Buy and Sell Stock IV
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
76 0
LeetCode 188. Best Time to Buy and Sell Stock IV
|
算法
LeetCode 123. Best Time to Buy and Sell Stock III
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
91 0
LeetCode 123. Best Time to Buy and Sell Stock III
|
算法
LeetCode 121. Best Time to Buy and Sell Stock
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果您最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
71 0
LeetCode 121. Best Time to Buy and Sell Stock
Leetcode-Easy 121. Best Time to Buy and Sell Stock
Leetcode-Easy 121. Best Time to Buy and Sell Stock
118 0
Leetcode-Easy 121. Best Time to Buy and Sell Stock
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2
|
5天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
38 7