【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 ...
随机推荐
- 使用MVVM框架avalon.js实现一个简易日历
最近在做公司内部的运营管理系统,因为与日历密切相关,同时无需触发条件直接显示在页面上,所以针对这样的功能场景,我就用avalon快速实现了一个简易日历,毕竟也是第一次造日历这种轮子,所以这里记录下我当 ...
- js DOM Element属性和方法整理
节点操作,属性 1. childNodes.children 这两个属性获取到的子节点会根据浏览器的不同而不同的,所以一定要判断下nodeType是否为1. childNodes获取到的是NodeLi ...
- 基础知识系列☞MSSQL→约束
遇到一个数据库设计很渣的系统··· 本来一个约束就能解决的问题·以前建库的时候也不设计好···
- ajxa
ajxa上传文件提交: ajxa跨域:http://www.cnblogs.com/sunxucool/p/3433992.html http://www.cnblogs.com/fsjohnhuan ...
- 使用PHP的五个小技巧
PHP的一些小技巧,比较基础,总结一下,老鸟换个姿势飘过去就是. 1. str_replace str_replace是非常常常常常用的php函数,用于字符串替换,经常看到某些php新人为了替换一批字 ...
- ubutu之qq安装
1.压缩包下载链接 http://pan.baidu.com/s/1nvlgsGh 2.安装教程(引用自百度经验) 如何在Ubuntu中安装QQ
- 在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突
- jQuery自诞生以来,版本越来越多,而且jQuery官网的新版本还在不断的更新和发布中,现已经达到了1.6.4版本,但是我们在以前的项目中就已经使用了旧版本的jQuery,比如已经出现的:1.3 ...
- mysql 设置可以外部访问
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; --授权可以外部 ...
- Win10主题打不开,自动弹出桌面图标设置
把官方下载的主题文件扩展名改成.deskthemepack,然后双击主题文件就可以正常使用了.
- Triangle
动态规划 int minimumTotal (vector<vector<int>>& triangle) { ; i >= ; --i) ; j < i ...