leetcode121—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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
想法:找到价格差最大即可
class Solution {
public:
int maxProfit(vector<int>& prices) {
;;
int minPrices = INT_MAX;
; i < prices.size() ; i++){
minPrices = min(minPrices,prices[i]);
maxPrices = max(maxPrices,prices[i]-minPrices);
}
return maxPrices;
}
};
leetcode121—Best Time to Buy and Sell Stock的更多相关文章
- Leetcode-121 Best Time to Buy and Sell Stock
#121 Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price ...
- LeetCode121: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 w ...
- [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 ...
随机推荐
- Java - TreeSet源码解析
Java提高篇(二八)------TreeSet 与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的.在<Java提高篇(二七)-----TreeMap& ...
- 【代码笔记】iOS-NSLog的使用
代码: // 在debug模式下输出NSLog,在release模式下不输出NSLog #ifndef __OPTIMIZE__ #define NSLog(...) NSLog(__VA_ARGS_ ...
- CADO SAP tcode - Time Sheet: Display Data
CADO (Time Sheet: Display Data) is a standard SAP transaction code available within R/3 SAP systems ...
- JSP内置对象——session对象
举个购物流程的例子: 这整个购物过程,它是属于一次回话.那么这个session是保存在服务器内存当中,并且它保存着不同用户对应的session,一个用户对应一个session.看下面这幅图: 从图中可 ...
- 关于Mobx中装饰器语法的环境配置
1.弹出项目配置 npm run eject 此处注意,若弹出项目配置失败,请先执行以下两行代码(若没有安装git则请跳过,本人是在安装git的情况下解决问题的) 1.git add . 2.git ...
- Httprunner学习
一.简介 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份YAML/JSON脚本,即可实现自动化测试.性能测试.线上监控.持续集成等多种测试需求. 核心特性: 继 ...
- 阿里云rds实例恢复到本地
摘要: 前提: 1,阿里云数据库备份实例,恢复数据的时候需要将数据恢复到本地数据库,是不能直接恢复到RDS上的. 2,需要在本地服务器上下载一个数据库,尽量和RDS数据库版本保持一致.(我现在用的是5 ...
- python数据类型之间的转换
1,字符串转整型,前提条件是该字符串为纯数字. a = '1' a = int(a) 2,整型转字符串 a= 1 a = str(a) 3,整型转浮点型 a = 1 a = float(a) 4,浮点 ...
- 解决VB6.0中不能加载MSCOMCTL.OCX的错误提示
VB6.0毕竟是很古老的开发工具了,其对所使用的第三方组件依赖性比较强,例如在打开从其它电脑上拿来的VB6.0的软件(系统)的工程文件(源代码)时,经常会遇到"不能加载MSCOMCTL.OC ...
- MySQL crash-safe replication(1)
MySQL 5.6 对复制功能提供了新特性:slave 支持 crash-safe,可以解决之前版本中系统异常断电可能导致的 SQL thread 信息不准确的问题. 原文:Enabling cras ...