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 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: 5
max. 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.
题解:关键点在于随着prices的遍历,更新成本cost和计算prices[i]和cost的差值。初始cost设置为最大int整数,profit初始为0,随着遍历的进行,计算prices[i]和当前cost的差值,如果差值小于0,则更新cost值为prices[i],如果差值大于当前profit,则更新profit的值为当前差值。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int cost = ;
int profit = ;
for (int i = ;i<prices.size();++i)
{
int p = prices[i] - cost;
if (p > profit)
profit = p;
if (prices[i] < cost)
cost = prices[i];
}
return profit;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<)
return ;
int cost=prices[];
int profit=;
for(int i=;i<prices.size();i++)
{
cost=min(prices[i],cost);
profit=max(profit,prices[i]-cost);
}
return profit;
}
};
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 ----- 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 最大收 ...
- LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- oracle系统表查询
oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments -- 查询本 ...
- Nginx 获取真实 IP 方案
问题根源: 基于七层的负载均衡系统,获取IP的原理都是通过XRI和XFF进行处理,从中选出“正常情况下”的源头IP,然而这两个Header都是普通的HTTP头,任何代理程序都可以轻易修改伪造它们,使得 ...
- POJ Ant Counting DP
dp[i][j]表示前i种蚂蚁组成元素个数为j的集合有多少种. 则dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][ max(0,j-a[i]) ...
- 用PHP实现URL转换短网址的算法示例
短网址就是把一个长的地址转换在超级短的网址,然后访问短网址即可跳转到长网址了,下面来看用PHP实现URL转换短网址的算法与例子. 短网址(Short URL) ,顾名思义就是在形式上比较短的网址.在W ...
- python学习之路-day2-pyth基础2
一. 模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...
- 对比poj3050
#include <stdio.h> const int MAXN = 10; const int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, ...
- __slots__ 属性绑定
s = Student() # 创建新的实例 s.name = 'Michael' # 绑定属性'name' s.age = 25 # 绑定属性'age' s.score = 99 # 绑定属性'sc ...
- iOS遍历程序内某个文件夹下所有文件的属性
项目中有个文件管理系统,在做本地文件管理操作的时候,遇到了遍历本地文件的问题 遍历到的文件有些不需要显示,而且需要得到文件的相关属性,在此总结下. //查找需要遍历文件夹的目录 NSString *k ...
- Highcharts图形报表的简单使用
Highcharts是一个纯JavaScript框架,与MSChart完全不一样,可以在网页中使用,所以php.asp.net.jsp等等页面中都可以使用.Highcharts官网:http://ww ...
- Activity的生命周期及各生命周期方法的作用
一.Activity的生命周期中各个方法的作用 onCreate(): 做Activity上所需要数据的初始化工作. onStart(): 显示Activity界面,此时用户对界面可见但不可交互. o ...