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 ...
随机推荐
- 特殊js事件
1:点击enter事件 $(document).keypress(function(e) { // 回车键事件 if(e.which == 13) { submitForm(); } }); 2:JQ ...
- SHA1算法
public string SHA1_Hash(string str_sha1_in) { SHA1 sha1 = new SHA1CryptoServicePro ...
- 自动化前端构建工具--gulp
Gulp是一个基于任务的javascript工程命令行流式构建工具.为什么要用Gulp呢?前端开发进入到工程化阶段,我们需要压缩合并文件,加MD5戳:如果使用 CoffeeScript/ES6 去代替 ...
- paramiko模块
安装: # pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto (1) wget http://ftp.dlitz.net/pub/dlitz/cr ...
- Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码
//调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...
- vb.net字符串格式转为日期型
vb.net字符串格式转为日期型 比如 "20080815" 转换为"2008-05-15"Dim a As Date Dim s As String = ...
- html5 自定义验证信息
h5 为表单新增了很多类型,及属性. 根据这些新增的类型及属性 h5也为我们提供了验证这些数据的js函数,这些验证表单的函数都存在了ValidityState对象中,接下来让我们一起来了解一下这些 ...
- 转载:Solr的自动完成实现方式(第三部分:Suggester方式续)
转自:http://www.cnblogs.com/ibook360/archive/2011/11/30/2269126.html 在之前的两个部分(part1.part2)中,我们学会了如何配置和 ...
- javascript 中的继承实现, call,apply,prototype,构造函数
javascript中继承可以通过call.apply.protoperty实现 1.call call的含义: foo.call(thisObject, args...) 表示函数foo调用的时候, ...
- 将Linux文件清空的几种方法
1.使用重定向的方法 [root@centos7 ~]# du -h test.txt .0K test.txt [root@centos7 ~]# > test.txt [root@cento ...