best-time-to-buy-and-sell-stock Java code
2017-10-24 13:10
387 查看
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).
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) { int maxProfit=0; if(prices.length<1) return 0; for(int i=1;i<prices.length;i++){ int diff=prices[i]-prices[i-1]; if(diff>0) maxProfit+=diff; } return maxProfit; } }
相关文章推荐
- best-time-to-buy-and-sell-stock Java code
- best-time-to-buy-and-sell-stock Java code
- Best Time to Buy and Sell Stock II leetcode java
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
- LeetCode 121. Best Time to Buy and Sell Stock 题解 —— Java
- Best Time to Buy and Sell Stock(JAVA)--动态规划
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
- LeetCode(123) Best Time to Buy and Sell Stock III (Java)
- Best Time to Buy and Sell Stock IV Leetcode Java
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
- 【leetcode】123. Best Time to Buy and Sell Stock III【java】
- leecode 解题总结:188. Best Time to Buy and Sell Stock IV
- [Leetcode] Best Time to Buy and Sell Stock (Java)
- 122. Best Time to Buy and Sell Stock II Java
- [LeetCode][Java] Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock III leetcode java
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
- leecode 解题总结:122. Best Time to Buy and Sell Stock II
- leetcode(121)(122)(123)(188) Best Time to Buy and Sell Stock I/II/III/IV JAVA代码