LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
分析
参考stock problem相关的精帖
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<=1)
return 0;
vector<int> sell(prices.size(),0),buy(prices.size(),0);
buy[0]=-prices[0];
for(int i=1;i<prices.size();i++){
sell[i]=max(sell[i-1],buy[i-1]+prices[i]);
buy[i]=max(buy[i-1],sell[i-1]-prices[i]);
}
return sell[prices.size()-1];
}
};
LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)的更多相关文章
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode 122 Best Time to Buy and Sell Stock II 解题报告
题目要求 Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Luogu P1186 玛丽卡 【最短路】By cellur925
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- mysql 循环批量插入
背景 前几天在MySql上做分页时,看到有博文说使用 limit 0,10 方式分页会有丢数据问题,有人又说不会,于是想自己测试一下.测试时没有数据,便安装了一个MySql,建了张表,在建了个whil ...
- WIN32 API ------ 最简单的Windows窗口封装类
1 开发语言抉择 1.1 关于开发Win32 程序的语言选择 C还是C++ 在决定抛弃MFC,而使用纯Win32 API 开发Window桌面程序之后,还存在一个语言的选择,这就是是否使用C++.C+ ...
- 持有对方的引用&&内部类
现在来做个很简单的东西,就是做一个做加法的图形界面 然后现在先是一个不用持有对方引用的写法: import java.awt.*; import java.awt.event.*; public cl ...
- 每天学点linux命令之locate 与 find 命令
定位某个文件的位置 方法一 sudo find / -name libGLEW.so.1.13 -type f 方法二 sudo updatedb && locate libopenc ...
- jquery + ajax 实现多条件查询
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JquerySort.aspx. ...
- Java的知识储备及面试-几个方面
1.Java本身语法基础 https://github.com/Snailclimb/JavaGuide/blob/master/Java%E7%9B%B8%E5%85%B3/Java%E5%9F%B ...
- Objective-C Foundation 框架 Example :Looking for Files 查找文件
Objective-C Foundation 框架 Example :Looking for Files 查找文件 NSFileManager. The NSFileManager class ...
- 洛谷——P2680 运输计划
https://www.luogu.org/problem/show?pid=2680 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每 ...
- hibernate inverse属性
修改街道对应的区道信息: 修改后会发现程序执行了两次修改操作: 原因: 区道与街道是一对多的关系: 由于Hibernate是双向维护外键,所以当修改区道中的街道时,会修改一次外键:在修改街道中的区道时 ...