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 ...
随机推荐
- html引用ttf字体文件
在样式表如此定义: @font-face { font-family: MyFontName;//自定义字体名称 src: url(../Gloss_And_Bloom.ttf) } 然后,具体使用: ...
- JS正则表达式验证(一)
目录: 手机号验证 固定电话验证 手机号验证: 写法[1]--->!(/^1[34578]\d{9}$/.test(phone)):以1开头,第二位可能是3/4/5/7/8等的任意一个,在加上后 ...
- getHiddenProp() 浏览器状态切换改变
<script> function getHiddenProp() { var prefixes = ['webkit', 'moz', 'ms', 'o']; // if 'hidden ...
- Java使用JNA方式调用DLL(动态链接库)(原创,装载请注明出处)
Java使用JNA调用DLL 1.准备 1.JDK环境 2.Eclipse 3.JNA包 下载JNA包: (1).JNA的Github:https://github.com/java-native-a ...
- MongoDB中mapReduce的使用
MongoDB中mapReduce的使用 制作人:全心全意 mapReduce的功能和group by的功能类似,但比group by处理的数据量更大 使用示例: var map = function ...
- buf.includes()
buf.includes(value[, byteOffset][, encoding]) value {String} | {Buffer} | {Number} byteOffset {Numbe ...
- ResNet实战
目录 Res Block ResNet18 Out of memory # Resnet.py #!/usr/bin/env python # -*- coding:utf-8 -*- import ...
- 11-看图理解数据结构与算法系列(B树的删除)
删除操作 删除操作比较复杂,主要是因为删除的项可能在叶子节点上也可能在非叶子节点上,而且删除后可能导致不符合B树的规定,这里暂且称之为导致B树不平衡,于是要进行一些合并.左旋.右旋等操作,使之符合B树 ...
- 开发调式时生成dump文件
开发调式时,对程序生成dump文件:1:需要生成的时机,加Thread.sleep(600*1000).2:打开jvisualvm找到该程序进程号.3:jmap.
- Phong 光照模型(镜面反射)
Phong 光照模型 镜面反射(高光),是光线经过物体表面,反射到视野中,当反射光线与人的眼睛看得方向平行时,强度最大,高光效果最明显,夹角为90度时,强度最小. specular = I*R*V: ...