/**
* 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. Vue 记录 Cannot read property '_withTask' of undefined

    第二次遇到,年前偶尔代码中频繁出现过,因为没影响到交互,赶工期中,没有去深究. 今天又遇到了, 在事件触发后,脚本报错,终止了界面交互. 最后查找到这里的原因,检查并移除无效业务事件,错误消失了. ( ...

  2. 10-HTTPServletReauest和HTTPServletResponse

    Servlet配置方式 1. 全路径匹配 以 / 开始 /a /aa/bb localhost:8080/项目名称/aa/bb 2. 路径匹配 , 前半段匹配 以 / 开始 , 但是以 * 结束 /a ...

  3. 初识Jmeter

    初识Jmeter 测试计划是根节点,其下可以有多个Thread Group,起始可配setUp Thread Group和tearDown Group.在每个Group下可创建其它节点,模拟各类实际行 ...

  4. 基础select语句详解

    在数据库操作语句中,使用最频繁,也被认为最重要的是 SELECT 查询语句.我们已经在不少地方用到了 SELECT * FROM table_name; 这条语句用于查看一张表中的所有内容. 而 SE ...

  5. Android Studio 去除上方标题

    选中代码再Ctrl+shift+'/' 可加/***/注释 https://blog.csdn.net/wuqingsen1/article/details/78554117 styles.xml & ...

  6. CROS+node-basis+ajax

    $.ajax({ url: this.baseUrl + this.restful.showDesigerViewList, type: "post", dataType: &qu ...

  7. Python-常用字符串操作

    name = 'shanbaoliang.exe' print(name.capitalize()) #将字符串首字母大写 print(name.center(50,'-')) #把字符串居中,并用特 ...

  8. Java作业六(2017-10-30)

    /*游戏引擎包,播放音乐*/ import com.rupeng.game.GameCore; public class Mc implements Runnable{ public static v ...

  9. 从协议入手,剖析OAuth2.0(译 RFC 6749)

    1.介绍      https://tools.ietf.org/html/rfc6749  传统的client-server授权模型,客户端通过使用凭证(通常的用户名和明文密码)访问服务端受保护的资 ...

  10. 小程序组件化框架 WePY 在性能调优上做出的探究

    作者:龚澄 导语 性能调优是一个亘古不变的话题,无论是在传统H5上还是小程序中.因为实现机制不同,可能导致传统H5中的某些优化方式在小程序上并不适用.因此必须另开辟蹊径找出适合小程序的调估方式. 本文 ...