博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ATT】Best Time to Buy and Sell Stock II
阅读量:5232 次
发布时间:2019-06-14

本文共 853 字,大约阅读时间需要 2 分钟。

Q: 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).

A: 可以买卖股票多次,同时如果已经买了股票,那么接下来只能先卖了股票再买股票,一天只能执行买卖一次。

int maxProfit(vector
&prices) { // Start typing your C/C++ solution below // DO NOT write int main() function if(prices.empty()) return 0; int diff; int mprofit = 0; for(int i=1;i
0) mprofit+=diff; } return mprofit; }

  

转载于:https://www.cnblogs.com/summer-zhou/p/3330095.html

你可能感兴趣的文章