[Leetcode] 122. Best Time to Buy and Sell Stock II
2015-04-07 02:12
253 查看
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).
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) { if(prices == null || prices.length == 0) return 0; int result = 0; for(int i = 1; i < prices.length; i++){ int diff = prices[i] > prices[i - 1]; if(diff > 0){ result += diff; } } return result; } }
相关文章推荐
- [LeetCode] Same Tree
- LeetCode: Container With Most Water
- LeetCode N-Queens
- leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)
- leetcode Longest Consecutive Sequence
- [LeetCode] Unique Binary Search Trees
- leetcode-3 closest
- LeetCode – Refresh – Best Time to Buy and Sell Stock ii
- [LeetCode]Count Primes
- leetcode 226 Invert Binary Tree 翻转二叉树
- leetcode--Word Break II
- Combination Sum 组合数求和-Leetcode
- LeetCode79——Word Search
- LeetCode:Find Peak Element
- leetcode 242. Valid Anagram
- LeetCode - Reverse Vowels of a String
- LeetCode-274&275.H-Index
- 【leetcode】Remove Duplicates from sorted array
- leetcode之网格从起点到终点的路径数之滚动数组
- [LeetCode] Can I Win 我能赢吗