1. 程式人生 > >LeetCode——121. Best Time to Buy and Sell Stock(買股票系列1)

LeetCode——121. Best Time to Buy and Sell Stock(買股票系列1)

一、題目121:

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., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
             Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

二、分析

最為簡單的賣股票問題,而且只能買賣一次,有點類似最大連續子串和題目(LeetCode53)

三、題解

class Solution {
public:
	int maxProfit(vector<int>& prices) {
		int ans = 0, tmp = 0;
		if (prices.size()<2)
			return ans;
		for (int i = 1; i < prices.size(); i++)
		{
			tmp = max(tmp + prices[i] - prices[i - 1], 0);   //類似連續子串最大和
			ans = max(ans, tmp);
		}
		return ans;
	}
};

相關推薦

LeetCode——121. Best Time to Buy and Sell Stock股票系列1

一、題目121: 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

leetcode 121 Best Time to Buy and Sell Stock最大盈利

題目要求(高頻題) 假設你有一個數組,其中第i個元素是第i天的股票價格。 如果您只被允許完成最多一筆交易(即買入並賣出一股股票),請設計演算法以找到最大利潤。 請注意,在購買之前不能出售股票。 解題思路 在陣列中尋找最大利潤,和leetcode 53 求最大子陣列是一種型別的題目

[LeetCode]121. 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 trans

LeetCode 121 Best Time to Buy and Sell Stock II 買賣股票的最佳時機 II

class Solution { public:     int maxProfit(vector<int>& prices) {         int sum=0;         for(int i=1;i<prices.size();i++)

[LeetCode] 121. Best Time to Buy and Sell Stock Java

most length 如果 時間復雜度 ase 最大差值 new [1] cas 題目: Say you have an array for which the ith element is the price of a given stock on day i.

leetcode 121. Best Time to Buy and Sell Stock

clas which style ces pre max des sign har Say you have an array for which the ith element is the price of a given stock on day i. If you

[leetcode] 121. Best Time to Buy and Sell Stock 解題報告

leet 解題報告 情況 lee 動態規劃 turn fit mat etc 動態規劃,註意處理當前最大值小於0的情況 public int maxProfit(int[] prices) { if (prices == null || prices.le

Leetcode 121: Best Time to Buy and Sell Stock

output inpu return ive price interview not don style Say you have an array for which the ith element is the price of a given stock on da

[LeetCode] 121. Best Time to Buy and Sell Stock 買賣股票的最佳時間

IT diff ger cti 利潤 變量 lee algorithm clas Say you have an array for which the ith element is the price of a given stock on day i. If you

LeetCode-121. Best Time to Buy and Sell Stock

0.原題 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

[leetcode]121. 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 mos

LeetCode121. 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

LeetCode#121: Best Time to Buy and Sell Stock

class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length

LeetCode--121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell Stock II

連續AC了5條medium有點疲憊,休息一下來兩條easy放鬆放鬆。 題目連結:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/和https://leetcode.com/problems/best-time-to-bu

LeetCode-面試演算法經典-Java實現】【121-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.

[LeetCode] 122. Best Time to Buy and Sell Stock II 買賣股票的最佳時間 II

complete design fit sha 一個 tran 利潤 多個 mes Say you have an array for which the ith element is the price of a given stock on day i. Design

[LeetCode] 188. Best Time to Buy and Sell Stock IV 買賣股票的最佳時間 IV

股票 ice not ++i int ase which before oba Say you have an array for which the ith element is the price of a given stock on day i. Design a

[LeetCode] 123. Best Time to Buy and Sell Stock III 買賣股票的最佳時間 III

AC range ger AR sign self you www release Say you have an array for which the ith element is the price of a given stock on day i. Design

leetcode 122. Best Time to Buy and Sell Stock II(買賣股票的最佳時機 II)

題目描述 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個演算法來計算你所能獲取的最大利潤。你可以儘可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。  示例 輸入:[7, 1,

[LeetCode] 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