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 ...
随机推荐
- 这是个简单的UTF8转码的小Demo
NSString *name = @"你好啊"; NSString *string = [NSString stringWithFormat:@"%@",nam ...
- STL中的set容器的一点总结
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
- php 之跨域上传图片
因为要将所有上传的图片上传到一台独立的图片服务器上面,js上传时存在跨域问题,网上找到这种,通过php curl方式,将图片重新发送到另外一台服务器上保存,并返回图片路径!这种方式存在一定问题:1,上 ...
- mysql实用操作
1.查看某个表的建表语句 show create table thetable -- thetable为待查表名
- java -cp
java -cp /home/hdp/log4jTest/log4j-1.2.17.jar:/home/hdp/log4jTest/testLog.jar:/home/hdp/log4jTest/co ...
- SNS网站成功原因剖析_完结
SNS网站成功原因剖析 前言 亿注册用户)为例,讨论下 Fackbook成功的原因,进而分析结合国内环境,讨论当前国内流行的 SNS网站成功失败要素. 一.Facebook (一) Facebook总 ...
- 打造一个有感觉的vim(四)
今天要介绍的VIM插件的名字叫做surround,这个插件的主要作用是将一个VIM的tex-objects(文本对象)添加或者取消包裹(单引号,双引号,XML标签等等)下载地址如下:https://g ...
- sql server之数据库语句优化
三.只返回需要的数据 返回数据到客户端至少需要数据库提取数据.网络传输数据.客户端接收数据以及客户端处理数据等环节,如果返回不需要的数据,就会增加服务器.网络和客户端的无效劳动,其害处是显而易见的,避 ...
- JS,JQuery的扩展方法
转 http://blog.csdn.net/tuwen/article/details/11464693 //JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展 ...
- Python3
1.上节内容回顾 递归: 明确的结束条件 问题规模每递归一次都应该比上一次的问题规模有所减少 效率低 高阶函数 文件: rb.wb.ab 一般用在不同系统之间传数据,和传视频流的时候用到,一般以这种形 ...