假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格。
设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。
详见:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/

Java实现:

方法一:

class Solution {
public int maxProfit(int[] prices) {
int n=prices.length;
if(n==0||prices==null){
return 0;
}
int profit=0;
for(int i=1;i<n;++i){
if(prices[i]>prices[i-1]){
profit+=prices[i]-prices[i-1];
}
}
return profit;
}
}

方法二:

class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n <= 1){
return 0;
}
int i = 0;
int profit = 0;
while(i < n - 1){
int buy,sell;
//寻找递减区间的最后一个值(局部最小点)
while(i+1 < n && prices[i+1] < prices[i]){
++i;
}
//局部最小点作为买入点
buy = i; //找下一个点(卖出点至少为下一个点)
++i;
//不满足。。继续往下找递增区间的最后一个值(局部最高点)
while(i<n && prices[i] >= prices[i-1]){
++i;
}
//设置卖出点
sell = i-1;
//计算总和
profit += prices[sell] - prices[buy];
}
return profit;
}
}

参考:https://www.cnblogs.com/grandyang/p/4280803.html

122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II的更多相关文章

  1. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...

  2. 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III

    假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...

  3. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

  4. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  5. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

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

  6. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  7. LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)

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

  8. 3.Best Time to Buy and Sell Stock(买卖股票)

    Level: ​ ​ Easy 题目描述: Say you have an array for which the ith element is the price of a given stock ...

  9. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

随机推荐

  1. POJ 2586 Y2K Accounting Bug(枚举大水题)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10674   Accepted: 53 ...

  2. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速

    E. Okabe and El Psy Kongroo     Okabe likes to take walks but knows that spies from the Organization ...

  3. sessionFactory的创建和四种查询方式

    1,关于sessionFactory的创建 5.0版本之前,下面这种方式在5.0及之后,可能会出问题,建议修改为5.0之后的方式 // 实例化Configuration Configuration c ...

  4. MapReduce算法形式二:去重(shuffle)

    案例二:去重(shuffle/HashSet等方法)shuffle主要针对的是key去重HashSet主要针对values去重

  5. poj 1180 Batch Scheduling (斜率优化)

    Batch Scheduling \(solution:\) 这应该是斜率优化中最经典的一道题目,虽然之前已经写过一道 \(catstransport\) 的题解了,但还是来回顾一下吧,这道题其实较那 ...

  6. finally{} 代码块

    package Exception; /* * finally{}代码块 * * finally{]代码块是必须要被执行的,不管异常是否处理成功,该代码块里面的代码体都会被执行, */ public ...

  7. JVM学习资料收集

    JVM实用参数(一)JVM类型以及编译器模式 http://ifeve.com/useful-jvm-flags-part-1-jvm-types-and-compiler-modes-2/ JVM实 ...

  8. p1697食物链

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说法 ...

  9. Hihocoder #1077 : RMQ问题再临-线段树(线段树:结构体建树+更新叶子往上+查询+巧妙使用father[]+线段树数组要开大4倍 *【模板】)

    #1077 : RMQ问题再临-线段树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到:小Hi给小Ho出了这样一道问题:假设整个货架上从左到右摆放了N种商品,并 ...

  10. html5--6-59 其他常用CSS属性

    html5--6-59 其他常用CSS属性 实例 学习要点 了解opacity属性:透明度设定 了解cursor属性:自定义鼠标样式 了解CSS新单位rem和em的区别 了解轮廓outline的设置 ...