题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/

题目大意:基本定义与121类似,不同点:121买卖股票只能有一次,且在这所有的一次买卖中找出最大利润值;122买卖股票不限次数,要求在这所有次数中的最大利润值(这里题目描述不准确,其实不是求某一次买卖得到的最大利润值,而是求所有次买卖所获得的总利润值)。

题解(借鉴):这里不用利用121的贪心思想,直接计算每一天中,只要比前一天贵,就卖出当前股票获取利润。(但是我觉得这里题目漏洞太大了,也就是说1,2,3这组数据,得到的最大值是2,由(2-1)+(3-2)得到)。代码如下(耗时1ms):

     public int maxProfit(int[] prices) {
int profit = 0;
for(int i = 1;i < prices.length; i++) {
if(prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}

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

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

  2. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  3. 122. Best Time to Buy and Sell Stock II@python

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

  4. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  5. 【刷题-LeetCode】122 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. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

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

  9. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  10. leetcode 122. Best Time to Buy and Sell Stock II ----- java

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

随机推荐

  1. bzoj3545-bzoj3551-Peaks

    题意 给出一个图,边有边权,点有点权,每次询问一个点 \(x\) 只走边权小于等于 \(d\) 的边能到达的点中点权第 \(k\) 大. 强制在线,\(n\le 10^5,m,q\le 5\times ...

  2. Qt消息机制和事件

    Qt消息机制和事件 1 事件 事件(event)是由系统或者 Qt 本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应 ...

  3. 基于centos系统安装pip模块

    pip模块安装 centos 6.5安装pip,centos安装Python包管理安装工具pip的方法如下: 此安装包使用的是pip1.5.5版本 wget --no-check-certificat ...

  4. open_basedir restriction in effect的错误及其解决办法

    问题是出现在了PHP.INI上面了   原因是php.ini里设置了     opendir=/var/web/w0895/:/tmp:/usr/lib/php  解答: 其实open_basedir ...

  5. hihocoder[Offer收割]编程练习赛19 D 相交的铁路线(树上路径交)

    傻逼题... 裸的树上路径交 两条树上的路径$[a,b]$和$[c,d]$有交,则有$lca(a,b)$在$[c,d]$上或$lca(c,d)$在$[a,b]$上. 其实只要深度大的$lca$在另一条 ...

  6. 【agc008F】Black Radius

    Portal --> agc008F Solution  这题好神仙啊qwq疯狂orz看懂日文题解的sjk太强啦qwq ​   ​  首先我们要统计的东西,是一个涂黑的连通块,然后我们考虑找一个 ...

  7. android ndk 编译的时候指令集的选取

    android ndk在编译的时候默认生成的是thumb指令(拇指)不是arm(手臂)指令,但是有时候在看反汇编的时候,不太熟悉thumb指令或者说thumb指令看起来更费劲,需要生成arm指令,这个 ...

  8. java发送邮件功能[转]

    原文链接:https://blog.csdn.net/jjkang_/article/details/56521959 Javamail遵循两个协议,一个是smtp协议,另一个是pop3协议.一般情况 ...

  9. Linux系统之路——如何在CentOS7.2安装MySQL

    一.Mysql 各个版本区别:1.MySQL Community Server 社区版本,开源免费,但不提供官方技术支持.2.MySQL Enterprise Edition 企业版本,需付费,可以试 ...

  10. uva 1639 Candy (对数处理精度)

    https://vjudge.net/problem/UVA-1639 有两个盒子各有n(n≤2*10 5 )个糖,每天随机选一个(概率分别为p,1-p),然后吃一颗糖. 直到有一天,打开盒子一看,没 ...