Best Time to Buy and Sell Stock II
2013-04-05 03:25
344 查看
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).
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 class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if (prices.length < 2) return 0; int profit = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] - prices[i - 1] > 0) profit += (prices[i] - prices[i - 1]); } return profit; } }
相关文章推荐
- leetcode ——Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
- LeetCode – Refresh – Best Time to Buy and Sell Stock ii
- leetcode Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II的C++解法
- Leetcode# 122 Best Time to Buy and Sell Stock II
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock I II III
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- leecode 解题总结:122. Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- 122 Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 【Leetcode长征系列】Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
- 122.leetcode Best Time to Buy and Sell Stock II(medium)[贪心算法 数组]
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告