Best Time to Buy and Sell Stock II (leetcode)

来源:互联网 发布:手机编辑照片软件 编辑:程序博客网 时间:2024/05/21 11:11

Say you have an array for which the ith element is the price of a given stock on dayi.

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_profit = 0;int len = prices.size();for (int i = 1; i < len; ++i) {if (prices[i] > prices[i - 1]) {max_profit += prices[i] - prices[i -1];}}return max_profit;    }};


0 0
原创粉丝点击