【数组】Best Time to Buy and Sell Stock I/II
Best Time to Buy and Sell Stock I
题目:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
思路:
只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o....i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
if(prices.length==0){
return 0;
}
var n=prices.length,low=2147483647,ans=0;
for(var i=0;i<n;i++){
if(prices[i]<low){
low=prices[i];
}else if(prices[i]-low>ans){
ans=prices[i]-low;
}
} return ans;
};
Best Time to Buy and Sell Stock I
题目:
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).
思路:
此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var n=prices.length,res=0; if(n==0){
return 0;
} for(var i=1;i<n;i++){
if(prices[i]>prices[i-1]){
res+=prices[i]-prices[i-1]
}
} return res;
};
【数组】Best Time to Buy and Sell Stock I/II的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- Best Time to Buy and Sell Stock I II III IV
一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
随机推荐
- 事务不起作用 Closing non transactional SqlSession
In proxy mode (which is the default), only external method calls coming in through the proxy are int ...
- 全面理解iOS开发中的Scroll View[转]
from:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方 ...
- (并查集 添加关系)How Many Answers Are Wrong --Hdu --3038
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- (最小生成树)QS Network -- ZOJ --1586
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...
- 老刘 Yii2 源码学习笔记之 Action 类
Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...
- 为MAC配置终端环境iTerm2+Zsh+oh-my-zsh
首先展示下我的终端吧. 这就是我们为什么要配置iTerm2+Zsh+oh-my-zsh环境的原因: 我们使用zsh解释器,当然等你使用 zsh时就会知道zsh与bash对比的强大之处了. 至于我们的g ...
- Flash(as3) 调整显示对象颜色
在游戏开发中改变显示对象的颜色是比较常见的操作,那么除了在FlashCS中调整,AS3又提供了怎样的方式呢? ColorTransform 这个类是轻量级的应用,其构造参数如下: ColorTrans ...
- Newtonsoft.Json日常用法
原文链接:https://www.cnblogs.com/ZengJiaLin/p/9578794.html
- div水平垂直居中方法及优缺点
代码: <div class="father"> <div class="son"> </div></div> ...
- day 104 luffy项目第二天
一.前端配置 二.后端配置 一.前端配置 app.vue 二 . 后端配置 model模型配置 迁移数据 序列化 views.py文件配置 url路由 配置中间件解决跨域问题 重新设计下 model模 ...