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 ...
随机推荐
- 常系数线性递推的第n项及前n项和 (Fibonacci数列,矩阵)
(一)Fibonacci数列f[n]=f[n-1]+f[n-2],f[1]=f[2]=1的第n项的快速求法(不考虑高精度). 解法: 考虑1×2的矩阵[f[n-2],f[n-1]].根据fibon ...
- HDU4845(SummerTrainingDay02-C 状态压缩bfs)
拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- 让一个小div在另一个大div里面 垂直居中的四种方法
方法一 .parent { width:800px; height:500px; border:2px solid #000; position:relative; } .child { width: ...
- (1)H5实现音乐播放器【正在播放-歌词篇】
近期闲来无事,就想着复习一下前端的东西,然后正好跟朋友搞了一个公共开放的音乐api接口,就想着写一个音乐播放器玩玩! 话不多说,直接上图,然后上代码 [播放器显示正在播放] 实现功能: 1:歌词随着歌 ...
- canvas绘画交叉波浪
做个记录,自己写的动态效果,可能以后用的着呢: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- 帆软发布大数据直连引擎FineDirect,对焦大数据BI
摘要:近日,帆软官方正式发布大数据直连引擎FineDirect模块.通过该模块,企业在应用FineBI原有功能的基础上,可直接对接现有数据源,无论是传统的关系型数据库,还是Hadoop生态圈.Mpp构 ...
- T-SQL的timestamp类型实际应用
目录 0x00 适用场景 0x01 问题描述 0x02 字节数组 0x03 Base64编码 0x04 其实没那么麻烦 0x05 回顾 0x00 适用场景 1. 前端: JavaScript 2. 后 ...
- 【Python】控制鼠标点击
from pymouse import PyMouse m = PyMouse() a = m.position() #获取当前坐标的位置 print(a) m.move(50, 500) #鼠标移动 ...
- matlab练习程序(单层感知器)
clear all; close all; clc; %生成两组已标记数据 randn(); mu1=[ ]; S1=[ ; ; 0.4]; P1=mvnrnd(mu1,S1,); mu2=[ ]; ...
- ecsop文件结构
Ecshop文件结构 :ecshop二次开发手册,ECSHOP文件结构,ECSHOP目录详解 /*ECShop 最新程序 的结构图及各文件相应功能介绍ECShop文件结构目录┣ activity.ph ...