假设有一个数组,它的第 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. IOS------Warning

    本文转载至 http://blog.csdn.net/shijiucdy/article/details/8755667 处理警告: 1,Validate Project Settings(updat ...

  2. JAVA运行时异常及常见的5中RuntimeExecption

    最近在抽时间看面试题,很多面试题都提出了写出java常见的5个运行时异常.现在来总结一下, java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了两种异常机制.一种是运行时异常 ...

  3. Window XP安装Ubuntu14.04实现Samba文件共享

    安装了Ubuntu14.04之后,在虚拟机设置里设置了文件共享.但在mnt文件夹下没有hgfs这个文件夹.依照网上说的去做还是不行,仅仅好放弃.改用samba实现Windows与Ubuntu文件共享. ...

  4. 缓存框架Ehcache相关

    单点缓存框架   只能针对单个jvm中,缓存容器存放jvm中,每个缓存互不影响  Ehcache gauva chache 内置缓存框架 jvm缓存框架 分布式缓存框架(共享缓存数据)  Redis ...

  5. html5--6-35 列表和表格

    html5--6-35 列表和表格 实例 学习要点 掌握列表和表格的样式设置 表格有关的属性: border-collapse 设置是否把表格边框合并为单一的边框.属性值:separate 默认值/c ...

  6. Silverlight中使用MVVM(3)

    Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...

  7. [Selenium] WebDriver 操作文件系统

    1)屏幕截图 接口函数是 TakesScreenshot 示例: import java.io.File; import org.apache.commons.io.FileUtils; public ...

  8. Struts2的各种标签库

    1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...

  9. vue之安装配置

    直接上图

  10. Watir: 当出现错误提示AutoItX3.dll 没有注册的时候,该怎么处理?

    对于Ruby 1.8版本,以管理员身份运行命令行窗口,输入Regsvr32 AutoItX3.dll路径即可.对于1.9 版本,路径与1.8版本是不同的,我们可以进入Ruby安装目录下,搜索AutoI ...