【LeetCode】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 to find the maximum profit.

思路:

只能买卖一次,所以要找一个最大差值的两天,但要注意低的那一天是在前面的。所以可以维护一个最小值和一个最大差值,从前往后走,如果比最小值小则更新,如果减最小值的差值大于之前的最大差值,则更新最大差值。

注意如果维护的是最大值和最小值,最后返回一个最大-最小的数,会是错的哦!

代码:

C++:

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

		int min_value = prices[0];
		int ans = 0;
		for(vector<int>::iterator it = prices.begin();it != prices.end();it++)
		{
			/* 更新最小值 */
			if(*it < min_value)
			{
				min_value = *it;
			}

			/* 更新最大差值 */
			if(*it - min_value > ans)
			{
				ans = *it - min_value;
			}
		}

		return ans;

    }
};

Python:

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        if len(prices) < 2:
            return 0
        min = prices[0]
        profit = 0
        for x in prices:
            if x < min:
                min = x
            if x - min > profit:
                profit = x-min
        return profit
时间: 2024-10-07 23:22:15

【LeetCode】Best Time to Buy and Sell Stock的相关文章

【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 IV

Best Time to Buy and Sell Stock IV 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 k transactions. Note: You may not engage in multiple t

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I 题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益. 分析:动态规划法.从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. 代码:时间O(n),空间O(1). Best Time to Buy and Sell Stock II 题目:用一

【leetcode】Best Time to Buy and Sell Stock III

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

【LeetCode】Best Time to Buy and Sell Stock III (2 solutions)

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

【题解】【数组】【DP】【Codility】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

【数组】Best Time to Buy and Sell Stock I/II

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 algo

【leetcode】Best Time to Buy and Sell 2(too easy)

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). Ho

【LeetCode】【Python题解】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). Ho