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).

解题思路:

仿照上题,如果遍历中出现下降,即卖掉同时将buy设置为下降点,JAVA实现如下:

    public int maxProfit(int[] prices) {
if(prices.length==0)
return 0;
int buy = 0,profit = 0;
for (int i = 1; i < prices.length; ++i) {
if (prices[i-1] > prices[i]){
profit+=prices[i-1]-prices[buy];
buy = i;
}
}
profit+=prices[prices.length-1]-prices[buy];
return profit;
}

Java for LeetCode 122 Best Time to Buy and Sell Stock II的更多相关文章

  1. 31. leetcode 122. Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  2. [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 ...

  3. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  4. leetcode 122. Best Time to Buy and Sell Stock II ----- java

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

  5. Java [Leetcode 122]Best Time to Buy and Sell Stock II

    题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  6. LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)

    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 122 Best Time to Buy and Sell Stock II 解题报告

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

  8. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

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

  9. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. centos7 samba安装与配置

    1.关闭防火墙. CentOS 7 是自带的firewall,CentOS 6 好像是iptables.关闭防火墙命令如下: 第一种方法是关闭防火墙: systemctl disable firewa ...

  2. Autolayout 02

    Working with Auto Layout Programmatically 如果你在运行阶段添加或者移除views你就需要通过代码来添加约束来保证你的interface能正确适应size或者o ...

  3. ios svn学习笔记(一)

    1, 遇到问题 git add in Xcode generates com.apple.dt.IDESourceControlErrorDomain error -70 这个错误发生在要右键选择要c ...

  4. weblogic的集群与配置图文方法

      一.Weblogic的集群 还记得我们在第五天教程中讲到的关于Tomcat的集群吗? 两个tomcat做node即tomcat1, tomcat2,使用Apache HttpServer做请求派发 ...

  5. Node.js自动化技术实现(Java)

    Node.js自动化测试框架(NodeTestFramework):

  6. AAuto如何设置定时器

    在设计视图中(一定要有个Form)点击左下角的功能组件   点击定时器即可切换到代码视图,并添加如下代码.其中我每隔一秒改变一下winform.static2.text的文本值      

  7. Vue 响应式数据说明

    值得注意的是只有当实例被创建时 data 中存在的属性才是响应式的.也就是说如果你添加一个新的属性,比如: vm.b = 'hi' 那么对 b 的改动将不会触发任何视图的更新. 这里唯一的例外是使用  ...

  8. 简单手机端头部设置 及css代码

    <html> <head> <title>今日报表</title> <meta http-equiv="Content-Type&quo ...

  9. Idftp.DirectoryListing 里面的内容为什么会是空的呢?(转)

    最近在项目中要用到FTP上传,用的是delphi的IdFTP控件,用IdFtp.List(list),发现List里面有内容,可 是到IdFtp.DirectoryListing.Items[iCou ...

  10. Net中的代码规范工具及使用

    Net中的代码规范工具及使用 https://www.cnblogs.com/selimsong/p/9209254.html 上一篇文章介绍了编码标准中一些常用的工具,本篇就具体来介绍如何使用它们来 ...