# leetcode中的股票问题(2)

## 原题

Say you have an array for which the ith$i^{th}$ element is the price of a given stock on day i$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).

## 代码

public int maxProfit(int[] prices) {
if(prices.length < 2)
{
return 0;
}
int sum = 0;
for(int i = 0; i < prices.length - 1; i++)
{
sum += prices[i + 1] - prices[i] > 0 ? prices[i + 1] - prices[i] : 0;
}
return sum;
}

3、4可能有点难哟，接下来~

• 本文已收录于以下专栏：

## [Leetcode]122. Best Time to Buy and Sell Stock II

122. Best Time to Buy and Sell Stock IISay you have an array for which the i-th element is the price...

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

## 和大神们学习每天一题(leetcode)-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...

## 121.122.123.188. Best Time to Buy and Sell Stock I II III IV

Say you have an array for which the ith element is the price of a given stock on day i. If you were...

## 【leetcode】Array——Best Time to Buy and Sell Stock I/II/III

Best Time to Buy and Sell Stock 题目： Say you have an array for which the ith element is the pri...

## LeetCode_Best Time to Buy and Sell Stock II

LeetCode_Best Time to Buy and Sell Stock II 解题思路

## LeetCode——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 a...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)