/**
* Source : https://oj.leetcode.com/problems/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 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 BestTimeToBuyAndSellStock2 { /**
* 找出最大可能的收益
* 可以交易多次,但是同一时间手中只能有一只股票
* 只要第二天的价格高于第一天,那么就可以在第一天买入,第二天卖出,这样就能保证每次赚钱,而且能赚到整个时间段内所有可能的利润
*
* @param prices
* @return
*/
public int maxProfit (int[] prices) {
int result = 0;
for (int i = 1; i < prices.length; i++) {
result += prices[i] > prices[i-1] ? prices[i] - prices[i-1] : 0;
}
return result;
} public static void main(String[] args) {
BestTimeToBuyAndSellStock2 bestTimeToBuyAndSellStock2 = new BestTimeToBuyAndSellStock2();
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{1,2,3,4,5}));
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{-2,1,3,5,-9,1,2}));
}
}

leetcode — best-time-to-buy-and-sell-stock-ii的更多相关文章

  1. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

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

  2. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  3. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  4. [LeetCode] 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 ...

  5. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

  6. [leetcode]Best Time to Buy and Sell Stock II @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...

  7. [LeetCode] 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 ...

  8. LeetCode: Best Time to Buy and Sell Stock II [122]

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

  9. LeetCode——Best Time to Buy and Sell Stock II

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

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

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

随机推荐

  1. iOS 上传自己的工程(模块工具类)到cocoapods上遇到坑

    最近在研究把自己写的工具类和模块上传到cocoapods上, 再新构建项目中可以直接使用cocoapods使用  也可以更新之前的版本 便于维护项目. 但是在这个过程中遇到了种种问题 但是最后还是解决 ...

  2. Hadoop namenode节点无法启动的问题解决

    namenode是Hadoop集群HDFS的管理节点,管理着整个分布式文件系统的命名空间,以及文件与块的映射关系等,在Hadoop集群中扮演着至关重要的作用. 我之前安装的Hadoop集群中namen ...

  3. 对象转JSON

    /// <summary> /// 把对象序列化 JSON 字符串 /// </summary> /// <typeparam name="T"> ...

  4. python学习:continue及break使用

    continue及break使用 #continue 作用:结束本次循环,继续下次循环#break 作用:跳出整个当次循环 for i in range(10): if i < 5: conti ...

  5. 最好的前端API备忘单整理

    注:这份表引自The best front-end hacking cheatsheets - all in one place Javascript ES2015 Cheatsheet JavaSc ...

  6. Linux的小知识点

    uname 2.whereis 3.df 4.which 5.apt和dpkg 6.service 7./etc/init.d/ 8.netstat -anptu 查看端口占用 9.netstat 1 ...

  7. python print 中文重定向失败

    一直以来认为解决python字符集编码,不一定需要通过sys.setdefaultencoding.因为既然python实现过程中,默认禁用了该操作,说明是不推荐的. 通过不断的字符转换,也cover ...

  8. 设置mysql InnoDB存储引擎下取消自动提交事务

    mysql 存储引擎中最长用的有两种,MyISAM 存储引擎和InnoDB存储引擎. 1.MyISAM 存储引擎 不支持事务,不支持外键,优势是访问速度快: 2.InnoDB存储引擎 支持事务,一般项 ...

  9. 配置 RIPv1 和 RIPv2

    拓扑图 场景您是公司的网络管理员.您所管理的小型网络中包含三台路由器,并规划了五个网络.您需要在网络中配置RIP路由协议来实现路由信息的相互传输.最初使用的是RIPv1,后来发现RIPv2更有优势,于 ...

  10. Java的几种设计模式

    java的设计模式大体上分为三大类: 创建型模式(5种):工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式. 结构型模式(7种):适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模 ...