本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43155725

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

思路:

(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,你可以完成多次交易,但是每次交易只能买入一次并卖出,求进行多次交易所能得到的最大利润。该题为Best Time to Buy and Sell Stock的加强版。

(2)与Best Time to Buy and Sell Stock类似,该题同样考查的是最大差值。只不过该题考查数组中所有相邻且递增元素的数值之差的总和。只要第i+1天的值大于第i天的值,则可买入,求得利润(差值),遍历整个数组,得到所用差值之和即为总的利润。

(3)该题还是比较简单。希望对你有所帮助。

算法代码实现如下:

 /*** * @author liqq*/public int maxProfit(int[] x) {if (x == null || x.length <= 1)return 0;int min = x[0];int profit = 0;for (int i = 0; i < x.length; i++) {if (min > x[i]) {min = x[i];} else {profit += x[i] - min;min = x[i];}}return profit;}

Leetcode_122_Best Time to Buy and Sell Stock II相关推荐

  1. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

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

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  3. 【Leetcode】122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II

    Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II买卖股票的最佳时机 II Say you have an array for which the ith ...

  4. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  5. 【leetcode-Python】-Dynamic Programming -122. Best Time to Buy and Sell Stock II

    目录 题目链接 题目描述 示例 解题思路 Python实现 时间复杂度与空间复杂度 Python实现·优化空间复杂度 时间复杂度与空间复杂度 解题思路二·贪心算法 Python实现 时间复杂度与空间复 ...

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

  7. C#LeetCode刷题之#122-买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4032 访问. 给定一个数组,它的第 i 个元素是一支给定股票第  ...

  8. LeetCode OJ - Best Time to Buy and Sell Stock II

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/33817395 Say you have ...

  9. leetcode python3 简单题122. Best Time to Buy and Sell Stock II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十二题 (1)题目 英文: Say you have an array pr ...

最新文章

  1. mysql备份数据库命令
  2. y sinx matlab,有一个函数 f(x,y)=x^2+sinxy+2y ,用matlab写一个程序 输入自变量的值,输出函数的值....
  3. 狂人日记学习 之一 CSS布局的目的
  4. python的decode函数报错_python2和python3的编码encode解码decode函数
  5. 01-10 Linux-bash编程
  6. 【NOI2002】【Luogu1196】银河英雄传说(并查集带边权)
  7. Android Studio 导入项目时容易出现的问题汇总
  8. input type=file与cursor:pointer的兼容性问题
  9. Linux下监测GPU温度指令
  10. python内置函数什么意思_python内置函数是什么
  11. 关于南京市大学生办理住房补贴的流程示意图
  12. java swing 插件下载_eclipse安装swing插件
  13. Ethereum/mist项目下Ethereum Wallet和Mist的区别
  14. python操作sqlite数据库
  15. webshell、木马与后门之间的区别
  16. #7 C++高级--内存管理、文件处理、多线程
  17. 函数节流(Throttle)和防抖(Debounce)解析及其OC实现
  18. unity 设置 异形按钮
  19. 【HTML】网页错误码详细报错
  20. 创业项目怎么获得专业的投融资服务?

热门文章

  1. 文本深度表示模型——word2vecdoc2vec词向量模型(转)
  2. SpringSecurity(二)、权限项目框架搭建
  3. win10专业版没有触摸板选项_Windows10触控板的正确使用方法
  4. python变量与声明
  5. 如何在win7下装ubuntu(硬盘版安装)
  6. typora:一些常用数学符号
  7. 大数据学习路线-入门精简
  8. 右键菜单“新建”的修改
  9. java sql server连接字符串_java连接sql server
  10. Java练习题 类 编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。