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定时程序,设定一个一直刷新,又时间间隔的js,读取当前的时间并显示
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- KMP算法分析
KMP是一种复杂度较低的字符串比较算法.基本思路是对欲匹配字符串进行预处理,分析当k位匹配时可以后移的位数,所得的数构成该字符串的特征向量. 求特征向量Next int* Next(string p) ...
- Centos 6.5 下php5.6.2 的编译安装
纯净的Centos 6.5系统 配置163yum源 (稍后会写一篇文档) 安装 开发软件包:yum -y groupinstall "Development Tools" 安 ...
- select 一直返回0
select设置超时时间后一直返回零,是因为每次select后监听的fd_set都被重置,解决方法就是每次重新设置
- SQL Server中Rowcount与@@Rowcount的用法
rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会 ...
- egrep 及扩展正则表达式
grep -E 表示支持扩展的正则表达式 grep -E = egrep 一.字符匹配: 扩展模式下的字符匹配与基本正则表达式的字符匹配相同,如: . 表示任意单个字符 [] 表示范围内人任意单个字符 ...
- 基于log4net的支持动态文件名、按日期和大小自动分割文件的日志组件
最近处理一个日志功能,用log4net的配置不能完全满足要求,所以在其基础上简单封装了一下,支持以下功能: 1 零配置 内置默认配置,引用dll后不需要添加或修改任何配置文件也可以使用 2 动态指定文 ...
- CheckedComboBoxEdit 重置初始化值的方法
CheckedComboBoxEdit ccbgb; ccbgb绑定方式ccbgb .Properties .DataSource =数据集; ccbgb.Properties.DisplayMemb ...
- Android 工程师如何快速学会web前段
Android 工程师如何快速学会web前段 今天主要聊一下本人最近在学习web前段的感受,最近html5是越来越火了,前段时间公司做了一个项目然后让我们“android”的程序猿过去帮忙把客户 端框 ...
- Excel 读写程序 C#源代码下载
http://u.163.com/lNaJAjOz 提取码: E4ZHjnfD