【一天一道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 最大收 ...
随机推荐
- JavaScript进阶-this
1.什么是this? 当一个函数被调用时,会创建一个活动记录(有时候也称为执行上下文).这个记录会包 含函数在哪里被调用(调用栈).函数的调用方法.传入的参数等信息.this 就是记录的 其中一个属性 ...
- jquery easyui datagrid设置可编辑行的某个列不可编辑
function onClickRowd(index1, field1) { if (editIndexd != index1) { if (endEditing()) { $('#dg').data ...
- thymeleaf:局部变量 th:with
当th:with被处理,firstPer变量创建一个局部变量和变量添加到map自上下文,以便它是用于评估和其他上下文中声明的变量从开始,但只有包含< div >标记的范围内. <di ...
- JSTL标签四种判断语句的用法
一.条件运算符 ${user.gender==1?'男':'女'} 二.if() <c:if test="${2>1}">code..</c:if> ...
- 关于一些基础的Java问题的解答(五)
21. 实现多线程的两种方法:Thread与Runable 在Java中实现多线程编程有以下几个方法: 1.继承Thread类,重写run方法 public class Test { public s ...
- Mysql各种引擎原理实战对比
1)存储引擎概述: (2)MySQL各大存储引擎: (3)InnoDB和MyIsam使用及其原理对比: (4)InnoDB和MyIsam引擎原理: (5)剩余引擎的使用DEMO(主要是Mrg_Myis ...
- MySQL数据库常用操作入门
一.MySQL MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.在WEB应用方面,MySQL是最好的RDBMS应用软件.MySQL体积小.速度快.总 ...
- Python List insert()方法详解
1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...
- 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)
总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...
- 【博弈】【HDU】取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...