leetcode笔记:Best Time to Buy and Sell Stock

一. 题目描述

Say you have an array for which the i-th 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.

二. 题目分析

题目的意思是输入一个表示一支股票每天股价的数组,第i个元素代表第i天的股价,只允许买入卖出一次,问怎么买卖使得收益最大?

首先想到的是把原始估价序列变成差分序列,则可转化为求数组的最大子段和。

或者,可以用类似动态规划的思想,假设在第i天买入,什么时候能赚到的最多的钱呢?不外乎就是在第i + 1n天中选择最大的股价减去第i天的股价。

三. 示例代码

// 第二种方法
#include <iostream>
#include <vector>
using namespace std;

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

        int maxPrice = prices[prices.size() - 1];
        int profit = 0;
        for(size_t i = prices.size() - 1; i >= 0; i--)
        {
            maxPrice = max(maxPrice, prices[i]);
            profit = max(profit, maxPrice - prices[i]);
        }
        return profit;
    }
};

四. 小结

与该题相关的题目还有好几道。后续更新…

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 04:08:50

leetcode笔记:Best Time to Buy and Sell Stock的相关文章

[LeetCode OJ] Best Time to Buy and Sell Stock I

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. eg:输

【LeetCode】Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha

Leetcode 贪心 Best Time to Buy and Sell Stock

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 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

[C++]LeetCode: 77 Best Time to Buy and Sell Stock II (贪心算法)

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times)

Leetcode problem-122 Best Time to Buy and Sell Stock II 题解

Leetcode Problem-122  Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, bu

LeetCode\EPI &quot;Best Time to Buy and Sell Stock&quot;

Also the very first problem on EPI. class Solution { public: int maxProfit(vector<int> &prices) { size_t len = prices.size(); if (len <= 1) return 0; else if (len == 2) { if (prices[0] >= prices[1]) return 0; else return prices[1] - prices

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode | 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Python】

LeetCode 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机[Easy][Python][贪心] Problem LeetCode 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 (i.e., b

[LeetCode][JavaScript]Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha

[LeetCode][JavaScript]Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock 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