best-time-to-buy-and-sell-stock-ii leetcode C++

mac2022-06-30  110

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

C++

class Solution { public: int maxProfit(vector<int>& prices){ int res = 0; int n = prices.size() - 1; for (int i = 0; i < n; ++i){ if (prices[i] < prices[i+1]) res += prices[i + 1] - prices[i]; } return res; } };

 

转载于:https://www.cnblogs.com/vercont/p/10210294.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)