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 ...
随机推荐
- SQLSTATE=2300
在powerdesigner将表的结构运用于数据库的时候报的错. 目标: 在Hibernate中使用多表级联的插入操作. 解决办法: 将navicat中的mysql数据库表删除, 手动创建 原因: p ...
- Kotlin – CharSequence IsNullOrBlank() vs IsNullOrEmpty()
本文摘自:http://blog.farifam.com/2018/01/28/kotlin-charsequence-isnullorblank-vs-isnullorempty/ Koltin p ...
- Beam Search
Q: 什么是Beam Search? 它在NLP中的什么场景里会⽤到? 传统的广度优先策略能够找到最优的路径,但是在搜索空间非常大的情况下,内存占用是指数级增长,很容易造成内存溢出,因此提出了beam ...
- kubernetes 知识点及常用命令
一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...
- mysql 删除数据重复的记录
delete from user where id not in ( select * from ( select min(id) from user group by username,email ...
- 第十九节:Scrapy爬虫框架之Middleware文件详解
# -*- coding: utf-8 -*- # 在这里定义蜘蛛中间件的模型# Define here the models for your spider middleware## See doc ...
- 89-Relative Vigor Index 相对活力指数指标.(2015.7.4)
Relative Vigor Index 相对活力指数指标 ~计算: RVI = (CLOSE-OPEN)/(HIGH-LOW) RVIsig=SMA(RVI,N) ~思想: 牛市中,收盘>开盘 ...
- 洛谷 3871 [TJOI2010]中位数
[题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...
- [WPF自定义控件库]为Form和自定义Window添加FunctionBar
1. 前言 我常常看到同一个应用程序中的表单的按钮----也就是"确定"."取消"那两个按钮----实现得千奇百怪,其实只要使用统一的Style起码就可以统一按 ...
- 关于java post get请求Demo (请求c#iis接口)
废话不多说,直接上代码 package dxq.httpGetDemo; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...