99_leetcode_Best Time to Buy and sell Stock
来源:互联网 发布:淘宝库存手表是真是假 编辑:程序博客网 时间:2024/05/24 00:58
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; }
0 0
- 99_leetcode_Best Time to Buy and sell Stock
- 110_leetcode_Best Time to Buy and sell Stock II
- 111_leetcode_Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Android改配置项(字体大小)后返回应用时程序(Fragment)崩溃
- As of ADT 14, resource fields cannot be used as switch cases.
- Eclipse 提示Adt 和 Andoroid Sdk 不匹配的临时解决方案
- asyn4j -- 异步方法调用框架
- 6. 面对对象(高级) Part 4 --- 学习笔记
- 99_leetcode_Best Time to Buy and sell Stock
- 微信营销怎么做?
- Java命名和目录服务(JNDI)
- soft skill
- 证明斯托尔兹定理及其一些有趣的应用
- jQuery基础---Ajax基础
- Android中RelativeLayout各个属性的含义
- RPC专题-Thrift与Protobuf比较2
- 单片机发展前景