【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 ...
随机推荐
- hdu 2189 来生一起走(DP)
题意: 有N个志愿者.指挥部需要将他们分成若干组,但要求每个组的人数必须为素数.问不同的方案总共有多少.(N个志愿者无差别,即每个组的惟一标识是:人数) 思路: 假设N个人可分为K组,将这K组的人数从 ...
- Win10下C语言转8086汇编
目录 Win10下C语言转8086汇编 简介 开始 写C代码 转换成汇编代码 Win10下C语言转8086汇编 简介 最近学习<王爽汇编>,然后突发奇想,想一边写C语言用编译器将其翻译成汇 ...
- Spring源码学习之容器的基本实现(一)
前言 最近学习了<<Spring源码深度解析>>受益匪浅,本博客是对学习内容的一个总结.分享,方便日后自己复习或与一同学习的小伙伴一起探讨之用. 建议与源码配合使用,效果更嘉, ...
- svn与git区别
代码扫描工具介绍:https://baijiahao.baidu.com/s?id=1629218655164599200&wfr=spider&for=pc Git和SVN的区别与联 ...
- ELK集群之grafana(8)
Grafana的安装和读取ES数据 模拟es数据产生sjgtest.py import time import datetime from elasticsearch import Elasticse ...
- TDSQL | 在整个技术解决方案中HTAP对应的混合交易以及分析系统应该如何实现?
从主交易到传输,到插件式解决方案,每个厂商对HTAP的理解和实验方式都有自己的独到解法,在未来整个数据解决方案当中都会往HTAP中去牵引.那么在整个技术解决方案中HTAP对应的混合交易以及分析系统应该 ...
- Qt Creator 源码学习笔记01,初识QTC
阅读本文大概需要 4 分钟 Qt Creator 是一款开源的轻量级 IDE,整个架构代码全部使用 C++/Qt 开发而成,非常适合用来学习C++和Qt 知识,这也是我们更加深入学习Qt最好的方式,学 ...
- Python 练习 人事管理
人事管理系统介绍:1.展示页面: ①首页: ==========欢迎来到简历管理系统v2.1.1========== 1.管理员登录 ...
- c语言循环位移(数字,字符串)
C语言中没有提供循环移位的操作符,但可以通过简洁的方式实现循环移位 设一个操作数x有s位则循环左移n位的操作为: (x << n) | (x >> (s - n)); 同理右移 ...
- VC练习一
1 #include<windows.h> 2 #include<stdio.h> 3 LRESULT CALLBACK WinSunProc(HWND hwnd,UINT u ...