99_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 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
1:遍历数组;2:每个数字获得该数字之前的最小的数字,并与当时保存的最大值相比較
int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int maxValue = 0;
int minPrice = prices[0];
int size = (int)prices.size(); for(int i = 1; i < size; i++)
{
if(prices[i] > minPrice)
{
maxValue = (maxValue > prices[i] - minPrice ? maxValue : prices[i] - minPrice);
}
else
{
minPrice = prices[i];
}
} return maxValue;
}
99_leetcode_Best Time to Buy and sell Stock的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
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 买卖股票的最佳时间之四
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 III 买股票的最佳时间之三
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 II 买股票的最佳时间之二
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 ...
- [LintCode] 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 ...
- [LintCode] 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——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- iOS缓存到sandbox
在手机应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在iOS设备中加一个缓存的机制,前面一篇文章介绍了iOS设备的内存缓存,这篇文章将设计一个本地缓存的机制. 功能需 ...
- Chrome插件:浏览器后台与页面间通信
content.js 与 background.js和popup.js 通信和 background.js与popup.js 这些通信都用 chrome.runtime.sendMessage 这个 ...
- jq进度条
<!doctype html><html><head><meta charset="utf-8"><title>JQue ...
- 洛谷——P3907 圈的异或
P3907 圈的异或 无向图$dfs$找环,并判断边权异或和是否为0 #include<iostream> #include<cstdio> #include<algor ...
- [Luogu] P4910 帕秋莉的手环
题目背景 帕秋莉是蕾米莉亚很早结识的朋友,现在住在红魔馆地下的大图书馆里.不仅擅长许多魔法,还每天都会开发出新的魔法.只是身体比较弱,因为哮喘,会在咏唱符卡时遇到麻烦. 她所用的属性魔法,主要是生命和 ...
- ehcache的学习笔记(一)
学习ehcache文档: 介绍:Ehcache是一个开源的项目,用来提高性能的基于标准化的缓存,无需使用数据库,简化了可扩展性.他是最广泛使用的基于java的缓存,因为他是强壮的,被证实的,功能全面的 ...
- mysql解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)的报错
一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...
- 75-ADMI,Average Directional Movement Index,平均方向性运动指标.(2015.7.1)
ADMI,Average Directional Movement Index 平均方向性运动指标 Directional Movement Index,平均方向性运动指标.(2015.7.1)&qu ...
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 【Codeforces 329B】Biridian Forest
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 找到出口到每个点的最短距离. 设你到出口的最短距离为temp 那么如果某个人到终点的距离<=temp,则他们肯定能遇到你 因为他们可以在 ...