【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
#
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
(二)解题
题目大意:给定一个数组表示一天之内的股价变化,只有一次买卖机会,如何做到利润最大。
解题思路:考虑采用两个指针i和j以及一个maxpro,第i时刻买,第j时刻卖(i
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
int maxpro = 0;//表示最大利润
int i = 0;//第i时刻买
for(int j = 1 ; j < n ; j++)
{
if(prices[j]>prices[i]){//当大于买时的价钱时
int temp = prices[j] - prices[i];//计算利润
maxpro = max(maxpro,temp);//更改maxpro,保存最大利润值
}
else
{
i=j;//新的买时刻
}
}
return maxpro;//返回最大利润
}
};
【一天一道LeetCode】#121. Best Time to Buy and Sell Stock的更多相关文章
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 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] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Java for LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Leetcode 121 Best Time to Buy and Sell Stock 动态规划
由于题意太长,请自己翻译,很容易懂的. 做法:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求.动态规 ...
- leetcode 121. Best Time to Buy and Sell Stock ----- java
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Python [Leetcode 121]Best Time to Buy and Sell Stock
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- [leetcode]121. Best Time to Buy and Sell Stock 最佳炒股时机
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
随机推荐
- c语言第六次作业v
(一)改错题 序列求和:输入一个正实数eps,计算序列部分和 1 - 1/4 + 1/7 - 1/10 + ... ,精确到最后一项的绝对值小于eps(保留6位小数). 输入输出样例: Input e ...
- DeepMoji:机器学习模型分析情绪, 情感
DeepMoji 是一个模型,接受12亿个带有表情的推文,以了解语言如何表达情绪. 通过转移学习,该模型可以在许多情感相关的文本建模任务上获得最先进的表现. 在 http://deepmoji.mit ...
- H3C交换机如何配置管理VLAN
1.输入"system-view"(简写"sys"),进入系统配置模式[H3C],2.下面就可以开始取消默认管理vlan了,输入"undo inter ...
- 获取X天后的日期
import java.util.Calendar; import java.util.Date; public class main { public static void main(String ...
- Centos Git1.7.1升级到Git2.2.1
安装需求: ># yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc ># ...
- bootstrap插件fileinput.js 出现出现$("#xxxx").fileinput({}); 不生效的情况解决
如果出现$("#xxxx").fileinput({}); 不生效的情况请将fileinput.js中最后几行注释掉: /* $(document).ready(function ...
- centos 6安装 H3C iNode 上网客户端
我的安装目录是/usr/iNode 直接把客户端安装包拷到这个文件夹下然后解压: #rar x iNode2.-R0162.rar 然后进入文件夹,里边有一个install.sh文件,这是一个安装文件 ...
- Radio Station
B. Radio Station time limit per test: 2 seconds memory limit per test: 256 megabytes input: standa ...
- IDEA中Git的使用
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...
- Nginx之(三)Nginx配置
一个简单的配置文件如下: #定义Nginx运行的用户及用户组 user userName userGroupName; #工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU worker_ ...