【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
dp_00 第i天 当前没有股票 状态是0
dp_01 第i天 当前持有股票 状态是1
class Solution {
public:
int maxProfit(vector<int>& prices) {
//经典的动态规划题目
if(prices.size()==0) return 0;
int dp_00=0;
int dp_01=INT_MIN;
for(auto it:prices)
{
dp_01=max(dp_01,-it);
dp_00=max(dp_00,dp_01+it);
}
return dp_00;
}
};
【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 最大收 ...
- LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- 聊了聊宏内核和微内核,并吹了一波 Linux
看这里!!!https://mp.weixin.qq.com/s?__biz=MzI0ODk2NDIyMQ==&mid=2247494048&idx=1&sn=cacfc6a4 ...
- 近期业务大量突增微服务性能优化总结-3.针对 x86 云环境改进异步日志等待策略
最近,业务增长的很迅猛,对于我们后台这块也是一个不小的挑战,这次遇到的核心业务接口的性能瓶颈,并不是单独的一个问题导致的,而是几个问题揉在一起:我们解决一个之后,发上线,之后发现还有另一个的性能瓶颈问 ...
- 找第k个结点 剑指22
这道题很简单,利用双指针. 主要是以下几个注意点 1. 判断链表是否为空 2. 判断k是否为0,若为0无意义 3.判断k是否超出了链表长度 /** * Definition for singly-li ...
- webpack 打包html文件
webpack 打包html文件 webpack.config.js配置文件内容为: /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 ...
- [python]RobotFramework自定义库实现UI自动化
1.安装教程 环境搭建不多说,网上资料一大堆,可参考https://www.cnblogs.com/puresoul/p/3854963.html,写的比较详细,值得推荐.目前python3是不支持r ...
- Buildroot 用户手册 (中文)
文章目录 I. Getting started 1. About Buildroot 2. System requirements 2.1. Mandatory packages 2.2. Optio ...
- sqlalchemy insert or ignore
insert ignore # insert ignoreinsert_stmt = TimePoint.__table__.insert().prefix_with(" ignore&qu ...
- 暑假算法练习Day1
为了加强个人的算法能力,所以准备每天都做适当的算法练习,并在隔天加以回顾. 依托PTA.Leetcode平台进行训练(暂定每天三题C++,对于Leetcode平台上的练习,由于其解题需以类的形式进行提 ...
- 部署一个支持Dapr 的Kubernetes APISIX Ingress
在这篇文章中,我将展示如何创建一个 APISIX控制器,该控制器在 Kubernetes 集群中公开启用 Dapr 的应用程序. 本质上,APISIX控制器将配置相同的标准 Dapr annotati ...
- 保姆级别的vue + ElementUI 搭建后台管理系统教程
vue + ElementUI 搭建后台管理系统记录 本文档记录了该系统从零配置的完整过程 项目源码请访问:https://gitee.com/szxio/vue2Admin,如果感觉对你有帮助,请点 ...