Best Time to Sell and Buy Stock
这道题想了很多,但是想多了。这个题思路很简单,如果当前值大于最小值,就计算差,和最大利润值比较。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==)
return ;
int length = prices.size();
int minVal = prices[];
int maxP = ;
for(int i=;i<length;i++)
{
if(prices[i]<minVal)
minVal = prices[i];
if(prices[i]-minVal>maxP)
maxP = prices[i]-minVal;
}
return maxP;
}
};
Best Time to Sell and Buy 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 ...
- 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 with Cooldown
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you hav ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- 309. 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 a ...
随机推荐
- Luogu P3960 列队(动态开点线段树)
P3960 列队 题意 题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia所在的方阵中有\(n \times m ...
- 解决C++ builder 4.0编译后的程序在某些计算机上运行出现"EAccessViolation" 的错误
1. bordbk41.dll is missing or not registered. regsvr32 "C:\Program Files (x86)\Common Files\Bor ...
- web前端学习(四)JavaScript学习笔记部分(8)-- JavaScript瀑布流
index.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...
- 异步 I/O 和事件驱动
异步IO(asynchronous I/O) 首先来理解几个容易混淆的概念,阻塞IO(blocking I/O)和非阻塞IO(non-blocking I/O),同步IO(synchronous I/ ...
- light oj 1149 Factors and Multiples(二分匹配)
LightOJ1149 :Factors and Multiples 时间限制:2000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 You w ...
- WPF 从属性赋值到MVVM模式详解
示例源码 这两天学习了一下MVVM模式,和大家分享一下,也作为自己的学习笔记.这里不定义MVVM的概念,不用苍白的文字说它的好处,而是从简单的赋值讲起,一步步建立一个MVVM模式的Simple.通过前 ...
- html文件中script标签放在哪里?
- Ubuntu 服务器默认的root账号是没有激活的,需要用初装的用户账号给root设置管理密码
user@ubuntu12:~$ sudo password root //用sudo修改账户 1.根据提示输入当前用户的密码: 2.修改成功之后你就可以使用root账号了,可以使用su root 命 ...
- 1、jxl导入/导出excel案例,黏贴即可运行
package junit.test; import java.io.File; import java.io.IOException; import java.util.ArrayList; imp ...
- Oracle锁表查杀会话进程
一.逐条--锁表 (1)查表名 和 sessionidselect b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_obj ...