122. Best Time to Buy and Sell Stock II

Easy

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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
  Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
  Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
  engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
package leetcode.easy;

public class BestTimeToBuyAndSellStockII {
@org.junit.Test
public void test() {
int[] prices1 = { 7, 1, 5, 3, 6, 4 };
int[] prices2 = { 1, 2, 3, 4, 5 };
int[] prices3 = { 7, 6, 4, 3, 1 };
System.out.println(maxProfit1(prices1));
System.out.println(maxProfit1(prices2));
System.out.println(maxProfit1(prices3));
System.out.println(maxProfit2(prices1));
System.out.println(maxProfit2(prices2));
System.out.println(maxProfit2(prices3));
System.out.println(maxProfit3(prices1));
System.out.println(maxProfit3(prices2));
System.out.println(maxProfit3(prices3));
} public int maxProfit1(int[] prices) {
return calculate(prices, 0);
} public int calculate(int prices[], int s) {
if (s >= prices.length) {
return 0;
}
int max = 0;
for (int start = s; start < prices.length; start++) {
int maxprofit = 0;
for (int i = start + 1; i < prices.length; i++) {
if (prices[start] < prices[i]) {
int profit = calculate(prices, i + 1) + prices[i] - prices[start];
if (profit > maxprofit) {
maxprofit = profit;
}
}
}
if (maxprofit > max) {
max = maxprofit;
}
}
return max;
} public int maxProfit2(int[] prices) {
if (null == prices || 0 == prices.length) {
return 0;
}
int i = 0;
int valley = prices[0];
int peak = prices[0];
int maxprofit = 0;
while (i < prices.length - 1) {
while (i < prices.length - 1 && prices[i] >= prices[i + 1]) {
i++;
}
valley = prices[i];
while (i < prices.length - 1 && prices[i] <= prices[i + 1]) {
i++;
}
peak = prices[i];
maxprofit += peak - valley;
}
return maxprofit;
} public int maxProfit3(int[] prices) {
int maxprofit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxprofit += prices[i] - prices[i - 1];
}
}
return maxprofit;
}
}

LeetCode_122. Best Time to Buy and Sell Stock II的更多相关文章

  1. [LintCode] 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 ...

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

  3. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  4. 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 pric ...

  5. 【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 ...

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

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

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

  9. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. EntityFramework 事物引发的问题

    前记 还是最近做的日志模块,今天做最后的入库工作.在测试入库日志记录时,总是出现怪异的问题. 开启服务开始接收 Kafka 的消息,第一条数据没有问题,后面的都如不了库.很是懵~~~ 调试了很久定位在 ...

  2. XSLT可扩展样式表语言转换 System.Xml.Xsl、XslCompiledTransform类

    XML文件 books.xml: <?xml version="1.0" encoding="utf-8" ?> <bookstore> ...

  3. Spring如何给静态变量注入值

    Common.java是一个工具类. Spring无法直接给静态变量注入值,因为静态变量不属于对象,只属于类,也就是说在类被加载字节码的时候变量已经初始化了,也就是给该变量分配内存了,导致spring ...

  4. 03_Tutorial 3: Class-based Views 基于类的视图(CBV)

    1.CBV 0.文档 https://q1mi.github.io/Django-REST-framework-documentation/tutorial/3-class-based-views_z ...

  5. JAVA的变量,数据类型与运算符

    1. 变量 计算机处理数据,变量被用来存储处理的数据,之所以叫做变量因为你可以改变存储的值.更确切的说,一个变量指向着一块存储特定类型值的地址,换句话说,一个变量有名称.类型和值.一个变量有一个名称, ...

  6. PostgreSQL 时间函数 extract函数

    计算时间差天数 select extract(day FROM (age('2017-12-10'::date , '2017-12-01'::date)));   计算时间差秒数 select ex ...

  7. 三十四.MySQL主从同步 、主从同步模式

    mysql51:192.168.4.51 主 mysql52:192.168.4.52 从 mysql50:192.168.4.50 客户机   1.MySQL一主一从   1.1 51,52 安装m ...

  8. 微信小程序的分享功能-css文字超过两行隐藏

    .info{ width:100px; word-break:break-all; display:-webkit-box; -webkit-box-orient:vertical; -webkit- ...

  9. python 元组 【基本使用功能】

    元组是括号,列表是方括号,都可以通用的有好多,比如判断一个元素是否存在可以直接用 in ,复制或者合并可以直接用乘或者加. 下面是在菜鸟教程截得的: 示例: #!/usr/bin/python # - ...

  10. Maven项目打包时指定配置策略

    以数据库连接池的配置文件(db.properties)为例,一般的项目会有开发用数据库,测试用数据库,正式环境数据库三种配置. 以前的做法是拷贝成三份,注释掉其他了两份 # 开发用 jdbc.url ...