Best Time to Buy and Sell Stock - LeetCode
题目链接
Best Time to Buy and Sell Stock - LeetCode
注意点
- 在卖出之前必须要先购入
解法
解法一:遍历一遍,随时记录当前数字之前的最小的数字。将当前数字与当前最小数字相减查看收益。时间复杂度O(n)
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
if(size < 1) return 0;
int max = 0,min = prices[0];
for(int i = 1;i < size;i++)
{
if(prices[i] - min > max) max = prices[i] - min;
if(prices[i] < min) min = prices[i];
}
return max;
}
};

小结
Best Time to Buy and Sell Stock - LeetCode的更多相关文章
- Best Time to Buy and Sell Stock——LeetCode
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 leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 121. Best Time to Buy and Sell Stock——Leetcode
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 LeetCode
LeetCode 我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益. 我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前 ...
- [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 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 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 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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- Linux下修改/设置环境变量JAVA_HOME
export设置只对当前的bash登录session有效.这是存在内存里面的.你可以写入文件一般的文件.之后source它.或者放到/etc/profile 等等的位置里,不同的地方效果不同. 1. ...
- Mybatis 中 columnPrefix别名的用法
1.映射对应的属性,区分他们分别属于哪些类.(sql书写的时候为什么要将前缀加上(别名),是因为便于它去寻找哪个类的前缀是ANNEX_) 2.例: 如下所示当一个collection 定义了一个co ...
- 递归遍历对象获取value值
let menu = { name: '一级菜单', data: { name: '二级菜单', data: { name: '三级菜单', data: { name: '四级菜单' } } } }; ...
- 记录网件r6220路由器登录配置
1.设置本地连接为自动获取ip和DNS地址 2.使用网线连接电脑和路由器的LAN口 3.http://routerlogin.net/BRS_index.htm 4.用户名和密码: admin pas ...
- Linux系统本地yum源环境配置记录
由于IDC的一些服务器没有外网,不能对外访问.所以打算部署一套内网的yum源环境,以供内网服务器使用.以下简单记录下操作过程: 1)下载centos6.9和centos7.3的镜像,并挂载 [root ...
- Docker容器学习梳理 - 基础环境安装
以下是centos系统安装docker的操作记录 1)第一种方法:采用系统自带的docker安装,但是这一般都不是最新版的docker安装epel源[root@docker-server ~]# wg ...
- Linux基础实践
Linux基础实践 1.1 应用安装 要求:掌握软件源的维护方法,配置系统使用软件源镜像.掌握通过软件源来查找,安装,卸载,更新软件的方法 备份原地址列表文件:sudo cp /etc/apt/sou ...
- 选择J2EE的SSH框架的理由
选择J2EE的SSH框架的理由 Struts2框架: Struts2框架的基本思想是采用MVC设计模式,即将应用设计成模型(Model).视图(View)和控制器(Control)三个部分:控制部分由 ...
- 第三个Sprint ------第五天
显示计算对错代码 package com.app.senior_calculator; import java.math.BigDecimal; import java.util.EmptyStack ...
- 如何使squild服务只能使用自定义的端口号
编辑配置文件: vim /etc/squid/squid.conf http_port 10000 使用 setsebool 命令来限制 squild 服务只能使用自定义的端口: setsebool ...