假设有一个数组,它的第 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. MapReduce算法形式二:去重(HashSet)

    案例二:去重(shuffle/HashSet等方法)shuffle主要针对的是key去重HashSet主要针对values去重

  2. Principle of least astonishment

    Principle of least astonishment - Wikipedia https://en.wikipedia.org/wiki/Principle_of_least_astonis ...

  3. Linux监控命令

    dd命令用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换.注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2它不是一个专业的测试工具,不过如果对于 ...

  4. G.易彰彪的一张表

    易彰彪最近有点奇怪,一向爱打游戏他最近居然盯着一张全是大小写字母的表在看,好像在找什么东西.他说,这是他女神给他的一张表,他需要回答女神的问题——在忽略大小写(即大写字母和小写字母视为同一字母)的情况 ...

  5. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

  6. Python学习笔记_Mysql数据库、Excel

    一.操作mysql数据库 import pymysql # 1.连上数据库:账号,密码,ip,端口号,数据库 # 2.建立游标(去数据库拿东西的工人) # 3.执行sql # 4.获取结果 # 5.关 ...

  7. hdu 1043 Eight(双向bfs)

    题意:经典八数码问题 思路:双向bfs ps:还有a*算法(还不会)等解法. 代码: #include<iostream> #include<stdio.h> #include ...

  8. QTextEdit/QPlainTextEdit添加文字超出视图后,滚动条自动移至最底部

    void ThreadExit::onTaskPerformState(const QString& strStatus) { //追加文本(ui.taskStatusTextEdit是一个Q ...

  9. function.py

    #文档字符串 def square(x): 'calculates the square of the number x' return x*x square.__doc__ help(square) ...

  10. iOS 堆和栈的区别和联系

    堆和栈的区别主要有以下五点: 1.管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来讲,释放工作由程序员控制,容易产生memory leak(内存泄露). 2.申请大小: 栈是向低地 ...