Leetcode解题笔记121. Best Time to Buy and Sell Stock [Easy] 动态规划
来源:互联网 发布:软件平台下载 编辑:程序博客网 时间:2024/06/06 00:50
解题思路
设v[i]为第i天前的购入最便宜值,则v[i] = min(v[i - 1], prices[i - 1]), 取一变量记录最大的prices[i] - v[i]即可。
代码
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty()) return 0; int maxW = 0; vector<int> v(prices.size()); v[0] = prices[0]; for (int i = 1; i < prices.size(); i++) { v[i] = min(v[i - 1], prices[i - 1]); maxW = max(prices[i] - v[i], maxW); } return maxW; }};
阅读全文
0 0
- Leetcode解题笔记121. Best Time to Buy and Sell Stock [Easy] 动态规划
- 121.LeetCode Best Time to Buy and Sell Stock(easy)[数组 动态规划]
- 动态规划-121. Best Time to Buy and Sell Stock
- 121. Best Time to Buy and Sell Stock 类别:动态规划 难度:easy
- 动态规划——121. Best Time to Buy and Sell Stock[easy]
- 【动态规划】Leetcode编程题解:121. Best Time to Buy and Sell Stock Add to List
- LeetCode笔记:121. Best Time to Buy and Sell Stock
- 【Leetcode-Easy-121】Best Time to Buy and Sell Stock
- 【Leetcode】121. Best Time to Buy and Sell Stock 【动态规划】
- ※ Leetcode - Dynamic Programming - 121. Best Time to Buy and Sell Stock(动态规划)
- 动态规划入门级教学(leetcode)121.Best Time to Buy and Sell Stock
- Leetcode(W8):121. Best Time to Buy and Sell Stock(动态规划)
- LeetCode-Best Time to Buy and Sell Stock-解题报告
- 【LeetCode】Best Time to Buy and Sell Stock 解题报告
- 【动态规划】Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV(动态规划)
- 121. Best Time to Buy and Sell Stock [easy] (Python)
- 121. Best Time to Buy and Sell Stock -Easy
- 登陆界面
- C语言学习之被吞掉的数组大小
- 关于多线程中使用SendMessage
- 蓝桥杯 Sine之舞
- Codeforces Beta Round #25 (Div. 2 Only)
- Leetcode解题笔记121. Best Time to Buy and Sell Stock [Easy] 动态规划
- 整理:Visual Studio Code (vscode) 配置C、C++环境/编写运行C、C++(Windows)
- idea最简单的免费要不要
- java实战 ——分类模块的开发
- 1283 最小周长
- Codeforces 151C Quantity of Strings(快速幂+数学分情况)
- 017大数据课程知识点小结
- 读过的最好的@(装饰器)文章
- springMvc 集成activti-modeler web流程设计器