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

LeetCode122: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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解題思路:

這題與上一題Best Time to Buy and Sell Stock相比,其實還更簡單,掃描一遍陣列,當前前元素大於前一個元素,則將其差值累加到max中,最後求得的max即為最大利益。

程式碼:

#include <iostream>
#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.

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).
However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).

*/ class Solution { public: int maxProfit(vector<int> &prices) { if(prices.empty()) return 0; int max = 0; for(int i = 1; i < prices.size(); i++) { if(prices[i] > prices[i-1]) max += prices[i] - prices[i-1
]; } 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; return 0; }

相關推薦

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

LeetCode122: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 compl

LeetCode122Best 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 profi

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

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

[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

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

122.Best Time to Buy and Sell Stock II

pan -i 題解 得到 sel 獲得 open 但是 blog 題目鏈接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 題目大意:基本定義與121類似,不同點:1

Leetcode 122: Best Time to Buy and Sell Stock II

com same clas multipl fit sel fin ans let Say you have an array for which the ith element is the price of a given stock on day i. Design

122. Best Time to Buy and Sell Stock II

find 設計 turn 之前 not div multiple ++ 再次 Say you have an array for which the ith element is the price of a given stock on day i. Design a

[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記-122. Best Time to Buy and Sell Stock II

i++ ++ 最大的 lee 技術分享 pre ice 為什麽 @param 今天題目如下: 要求出最大的利益。這題個人不太想得通,看了答案也不太知道為什麽這樣能獲得最大值。代碼如下: 1 /** 2 * @param {number[]} prices 3

Array——LeetCode——Best Time to Buy and Sell Stock II

price time [] turn pan 價格 pri tmp span 【學到的知識點——】 ---------------------------------------------------------------------------------------

LeetCode 122. 買賣股票的最佳時機 IIBest Time to Buy and Sell Stock II

必須 toc for pub i++ pre 價格 股票 時機 題目描述 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個算法來計算你所能獲取的最大利潤。你可以盡可能地完成更多的交易(多次買賣一支股票)。 註意:你不能同時參與多筆交易(你必須在

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

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

【LeetCode】116.Best Time to Buy and Sell Stock II

題目描述(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