1. 程式人生 > >LeetCode121:Best Time to Buy and Sell Stock

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

解題思路:

這道題不難,最簡單的解題思路複雜度為O(n2),不過我想應該會超時,這裡採用另一種解題方法,複雜度為O(n2

)。

採用兩個指標i和j,i用來指向所遍歷過的元素中最小值,j則用來遍歷陣列,然後用j指向的值與i指向的值相減,如果差值大於max,則替換max,當j指向的值小於i指向的元素時,將i = j,繼續之前的動作。

程式碼:

#include <iostream>
#include <climits>
#include <vector>
using namespace std;

/**
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.

*/ class Solution { public: int maxProfit(vector<int> &prices) { if(prices.empty()) return 0; int i = 0; int j = i+1; int max = 0; while(j < prices.size()) { if(prices[j] < prices[i]) { i
= j; } else { int t = prices[j] - prices[i]; if(t > max) max = t; } j++; } return max; } }; int main(void) { int arr[] = {2,4,5,1,7,10}; int n = sizeof(arr) / sizeof(arr[0]); vector<int> stock(arr, arr+n); Solution solution; int max = solution.maxProfit(stock); cout<<max<<endl; return 0; }

相關推薦

leetcode121Best 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 (i.e

LeetCode121. Best Time to Buy and Sell Stock

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.

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 you were only permitted to complete at most one transactio

LeetCode121Best 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

Leetcode Best Time to Buy and Sell Stock II

con max and [] python self leet sel ice class Solution: # @param {integer[]} prices # @return {integer} def maxProfit(sel

[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] 22. Best Time to Buy and Sell Stock II Java

股票 log ive highlight transacti ever 方法 size 可能 題目: Say you have an array for which the ith element is the price of a given stock on day i

14.Best Time to Buy and Sell Stock

pub 規劃 clas share nbsp return let element his 題目描述: Say you have an array for which the ith element is the price of a given stock on day

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——Best Time to Buy and Sell Stock III 買賣股票最大收益(AC)

element cti () -- 最大 leetcode price imu cto Say you have an array for which the ith element is the price of a given stock on day i. D

[LeetCode] Best Time to Buy and Sell Stock II

array tran toc public ans arr action let turn Say you have an array for which the ith element is the price of a given stock on day i. Des

[LeetCode] Best Time to Buy and Sell Stock

lee share ice com 使用 nbsp leetcode example != Say you have an array for which the ith element is the price of a given stock on day i. If

LeetCode——Best Time to Buy and Sell Stock II

股票 this mil ng- posit ria 時間 tco ++ Say you have an array for which the ith element is the price of a given stock on day i. Desi

&lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

今天 tar article 必須 font details let have 獲得 Say you have an array for which the ith element is the price of a given stock

[Array]122. Best Time to Buy and Sell Stock II(obscure)

nbsp 實現 元素 you max -s script -1 times Say you have an array for which the ith element is the price of a given stock on day i. Design an

Best Time to Buy and Sell Stock III

urn 曾經 -s 查找 tip 依據 都是 sign snippet Say you have an array for which the ith element is the price of a given stock on day i. Design

leetcode - Best Time to Buy and Sell Stock II

color return align eve like art pro position sign Say you have an array for which the ith element is the price of a given stock on

LeetCode -- Best Time to Buy and Sell Stock II

ice sel return fin ransac ret 難度 stock share 題目描寫敘述:Say you have an array for which the ith element is the price of a given stock on da

【算法分析與設計】【第一周】121.Best Time to Buy and Sell Stock&122. Best Time to Buy and Sell Stock II

部分 簡化 是我 -i 復雜 style 代碼 求一個 時間 原題來自:121:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 122:https://leetcode.c

121. Best Time to Buy and Sell Stock【easy】

logs ray indices found dex ret mut allow present 121. Best Time to Buy and Sell Stock【easy】 Say you have an array for which the ith el