Leetcode 121 Best Time to Buy and Sell Stock 动态规划
由于题意太长,请自己翻译,很容易懂的。
做法:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。动态规划的思路下次有空写个专题
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() < ) return ;
int maxProfit = ;
int curMin = prices[];
for (int i = ; i < prices.size(); i++) {
curMin = min(curMin, prices[i]);
maxProfit = max(maxProfit, prices[i] - curMin);
}
return maxProfit;
}
};
Leetcode 121 Best Time to Buy and Sell Stock 动态规划的更多相关文章
- 30. 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 of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [LeetCode] 121. 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 121. 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 for LeetCode 121 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 121. Best Time to Buy and Sell Stock ----- java
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Python [Leetcode 121]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 ...
- [leetcode]121. 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 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
随机推荐
- 数据库管理员<三>
数据库管理员 介绍 每个 oracle 数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个 dba 就够了,但是对于一个大的数据库 可能需要多个 dba 分担不同的管理职责.那么 ...
- iOS键盘出现时界面跟着往上推
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UI ...
- WX program
- mybatis 控制台打印sql
开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...
- linux下挂载新硬盘
挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 1.创建新硬盘分区 用fdisk + 路径 进行分区 进入磁盘,对磁盘进行分区 #fdisk /dev/sdb Command (m for h ...
- MSSERVER创建链接服务器
exec sp_addlinkedserver 'DB_RASS','','SQLOLEDB','127.0.0.1' ' exec sp_serveroption 'DB_RASS','rpc ou ...
- Eclipse的安装和java环境变量的设置
首先准备工作是要下载好Eclipse和java JDK. 必须要注意的是,Eclipse和java JDK必须下载同一位数的版本,即64位同为64位,32位同为32位.否则在安装完成运行Eclipse ...
- JS手机端去除默认自带的选择复制菜单
在需要的div上添加以下控制-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: ...
- Linq ExecuteQuery,ExecuteCommand
//连接语句 public readonly string sqlconn = ConfigurationManager.ConnectionStrings["Transaction_9_3 ...
- SQL Develop SSH远程连接
目前碰到了本机无法直接连接数据库服务器,然后mac上可以选择的oracle客户端又很少,直到知道了可以ssh远程到跳板机上,然后进行连接. 在跳板机上输入命令如下: ssh -L 0.0.0.0:65 ...