122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
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 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 Solution {
public int maxProfit(int[] prices) {
int profit=0;
for(int i=0;i<prices.length-1;i++){
int tmp=prices[i+1]-prices[i];
if(tmp>0){
profit+=tmp;
}
}
return profit;
}
}
一次过 哦也
空间复杂度o(1)级别 遍历一次时间复杂度0(n) 直接就是最优解了 不用看discuss了 继续看下一题
122. Best Time to Buy and Sell Stock(二) leetcode解题笔记的更多相关文章
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 122. Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 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 ...
- 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 ...
- [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 ...
- 【刷题-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 ...
随机推荐
- C#图片上传服务器缩放存储功能
项目需求上需要用户上传头像.本来是用第三方的插件的.但是很多都是收费的. 不过,我需要花这钱么?是不?所以网络上搜集了些资料.. 果然.这个世界 大神是很多很多的. 用了个免费插件. <scri ...
- openssh升级至7.2
此处升级操作的原则是保留系统原有ssh服务,新安装高版本ssh服务 1.下载openssh源码包 http://www.openssh.com/portable.html 2.安装#tar zxvf ...
- 并发下常见的加锁及锁的PHP具体实现-转载
并发下常见的加锁及锁的PHP具体实现 http://www.cnblogs.com/scotoma/archive/2010/09/26/1836312.html 在最近的项目中有这样的场景 1.生成 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- 向python文件传递参数
1. 向python传递单个参数: import sys print sys.argv[0] ##脚本名 print sys.argv[1] ## 第一个参数 2. 向python传递数组: pr ...
- chrome浏览器调用 ajax 提示net::ERR_INCOMPLETE_CHUNKED_ENCODING问题解决方案,以及 Response.Close 和 Response.End 的一些问题。
近段时间去了一家新公司任职,公司产品是一个网站,但是我发现它不兼容谷歌浏览器,用习惯了chrome的我简直是如鲠在喉.终于我抽出了时间,想纠正这个问题.F12一看,发现谷歌提示:net::ERR_IN ...
- Maven项目导入后打红色X
在所有的配置都正确的情况,程序能正常运行跑起来,看一下problem下的红色错误 如果这两个错误不影响你的程序,或者已经配置或处理,直接右击删除这两个错就行了. 删除之后,就没有了吧,OK搞定
- (转载)iOS 极光推送SDK 集成指南
iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...
- 初学c# -- 学习笔记(二)
接着前面的学习,对话建立了,下面就写对话框气泡,和微信的差不多那种.尖角对话气泡网上一堆,圆尖角的修改了一个.IE8以下不能用,其他都可以用,直接上html代码,将<style>内容用到你 ...
- eclipse导出jar包的方法
第一:导出基本项目的jar包(此项目没有引用过第三方jar包) 1.在eclipse中选择你要导出的项目或package或类,右击,选择Export子选项: 2.在弹出的对话框中,选择java文件-- ...