CODE 11: Best Time to Buy and Sell Stock II
来源:互联网 发布:大肚孕照软件下载 编辑:程序博客网 时间:2024/05/17 21:41
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).
public int maxProfit(int[] prices) {// Start typing your Java solution below// DO NOT write main() functionif (null == prices || prices.length <= 1) {return 0;}int[] diff = new int[prices.length];int max = 0;for (int i = 1; i < prices.length; i++) {diff[i] = prices[i] - prices[i - 1];}for (int i = 1; i < diff.length; i++) {if (diff[i] > 0) {max += diff[i];}}return max;}
- CODE 11: Best Time to Buy and Sell Stock II
- [leet code] Best Time to Buy and Sell Stock II
- CODE 12: Best Time to Buy and Sell Stock
- CODE 10: Best Time to Buy and Sell Stock III
- [leet code] Best Time to Buy and Sell Stock
- 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 Java code
- [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
- [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
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 倒萨解放路撒旦教覅家盛大
- 0915
- 使用GridView来获取xml文件数据
- rrtrtyyhh
- python容器排序sort()和内建函数sorted()的区别
- CODE 11: Best Time to Buy and Sell Stock II
- android Camera捕获数据
- #pragma 预处理指令详解
- 最长公共子序列模板
- 经典SQL语句大全
- Android动态加载jar、apk的实现
- JS怎么刷新当前页面
- [各种面试题] 困难的数
- CODE 10: Best Time to Buy and Sell Stock III