【leetcode】Best Time to Buy and Sell (easy)
题目:
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.
说白了,就是找一组数字里最大差值,并且大数要在小数的后面。 因为只能做一次买卖操作。
思路:从后向前记录最大的数和该最大数前面的最小数,不断更新最大差值。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
{
return ;
}
int maxNum = prices.back();
int minNum = prices.back();
int maxprofit = ;
vector<int>::iterator it;
for(it = prices.end() - ; it >= prices.begin(); it--)
{
if(*it < minNum)
{
minNum = *it;
}
else if(*it > maxNum)
{
maxprofit = ((maxNum - minNum) > maxprofit) ? (maxNum - minNum) : maxprofit;
maxNum = *it;
minNum = *it;
}
}
maxprofit = ((maxNum - minNum) > maxprofit) ? (maxNum - minNum) : maxprofit;
return maxprofit;
}
}; int main()
{
Solution s;
vector<int> vec;
int n = s.maxProfit(vec); return ;
}
【leetcode】Best Time to Buy and Sell (easy)的更多相关文章
- 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...
- 【leetcode】Best Time to Buy and Sell 2(too easy)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】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 ...
- 【数组】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 o ...
- 【Leetcode】【Medium】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 透过统计力学,模拟软物质——EPJE专访2016年玻尔兹曼奖得主Daan Frenkel
原文来源:Eur. Phys. J. E (2016) 39: 68 2016年玻尔兹曼奖得主Daan Frenkel接受欧洲物理学报E专访,畅谈统计物理在交叉科学研究中的前所未有的重要性. 统计物理 ...
- zookeeper集群配置与启动——实战
1,准备: A:三台linxu服务器: 10.112.29.177 10.112.29.172 10.112.29.174 命令 hostname 得到每台机器的 hostname vm-10-112 ...
- 自己总结的USB数据结构及其描述符
背景: USB理论知识光看着空想总觉着丢三落四,好像哪里没法理解到位,自己做个总结. 正文: 1. USB通信的最基本单位是“包”.如果把“包”肢解的话,可以分为各种“域”(7类,即一串二进制数.每类 ...
- 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
由于给予REST的Web服务非常简单易用,它越来越成为企业后端服务集成的首选方法.本文这里介绍一下如何通过微软的Asp.Net WebAPI快速构建REST-ful 服务. 首先创建一个Asp.Net ...
- svn更改默认服务启动目录
配置文件位于 /etc/sysconfig/svnserve 修改为自己的目录
- webpack 教程 那些事儿06-gulp+webpack多页
本篇主要讲述用gulp+webpack构建多页应用 折腾到现在,项目还必须要进行,.vue文件必须要加载,也就是webpack必须引入.时间不多了,抛弃上个方案之后,只能牺牲热加载性能,用gulp+w ...
- Error 0x800704cf
重装了系统,改过网络配置,结果共享和打印机连不上,显示Error 0x800704cf 在本地连接的属性里将"Client for Microsoft Networks"勾选上就可 ...
- session会话复制
前几天请教了Yoda大神关于会话复制方面的问题, 如果客户访问是两个服务器的东西,这样会有两个分别的session, 这两个session之间怎么交互,集群环境下,同构的服务器,专业术语 ...
- NOIP2016题目整合
今天终于拿到官方数据,兴致勃勃地全 A 了. Day 1 T1 toy 处理一下正负号加加减减取模乱搞就好了. #include <iostream> #include <cstdi ...
- 安装rabbitmq以及python调用rabbitmq--暂欠
一.安装erlang yum install erlang 二.安装rabbitmq rpm包: wget http://www.rabbitmq.com/releases/rabbitmq-serv ...