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的更多相关文章

  1. 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 ...

  2. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  3. 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 ...

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  5. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  6. 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 ...

  7. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. MATLAB实现最优低通滤波器的函数

    MATLAB实现最优低通滤波器的函数 % Fs     --Data rate % Fpass  --pass band % Fstop  --Cutoff frequencies % Apass  ...

  2. dos常用命令使用说明

    cd 改变当前目录 sys 制作DOS系统盘 copy 拷贝文件 del 删除文件 deltree 删除目录树 dir 列文件名 diskcopy 制磁盘 edit 文本编辑 format 格式化磁盘 ...

  3. 3) Maven 目录结构

    进入maven根目录 cmd 命令 tree E:. │ LICENSE.txt │ NOTICE.txt │ README.txt │ ├─bin │ m2.conf │ mvn │ mvn.bat ...

  4. hdu2571 命运 2016-09-11 16:54 53人阅读 评论(0) 收藏

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  5. Oracle SQL Trace 和 10046 事件

    http://blog.csdn.net/tianlesoftware/article/details/5857023 一. SQL_TRACE 当SQL语句出现性能问题时,我们可以用SQL_TRAC ...

  6. 更改GeoServer的端口号

    更改GeoServer的端口号,这一问题在不同的GeoServer版本上的解决办法不禁相同.本文记录GeoServer2.7.6(独立安装)版本更改其端口号的办法. GeoServer默认端口为808 ...

  7. KVM NAT网络模式配置

    NAT方式原理 NAT方式是kvm安装后的默认方式.它支持主机与虚拟机的互访,同时也支持虚拟机访问互联网,但不支持外界访问虚拟机. 检查当前的网络设置: #virsh net-list --all N ...

  8. Reddit CEO亲自诠释内容审核的无奈

    本文由  网易云发布. 导语:继数据泄露危机之后,Facebook将会雇用数千名新员工来负责新的验证系统,这个系统将首先在美国广告客户中生效,并将在未来几个月内涵盖其他国家.与此同时,如何让自己的社区 ...

  9. C++ STL的容器类型

    1.顺序容器 2.关联容器 3.vector的使用 vector<数据类型> a; a.push_back(10)  -------->把数据从末末尾段插入vector里面 a.po ...

  10. biz_platform项目过程

    1.前台界面主要采用React框架.通过Ajax方式将数据与tornado服务器交互.以下代码为请求后台数据. var ThisPage = React.createClass({ render: f ...