【leetcode】Best Time to Buy and Sell Stock II
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 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).
class Solution {
public:
int maxProfit(vector<int> &prices) { int profit=;
for(int i=;i<prices.size();i++)
{
if(prices[i]>prices[i-])
{
profit+=prices[i]-prices[i-];
}
}
return profit; }
};
class Solution {
public:
int maxProfit(vector<int> &prices) { int profit=;
int n=prices.size(); if(n==)
{
return ;
}
int buyPrice=prices[]; for(int i=;i<n;i++)
{
if(prices[i]<prices[i-])
{
profit+=prices[i-]-buyPrice;
buyPrice=prices[i];
}
} profit+=prices[n-]-buyPrice; return profit; }
};
【leetcode】Best Time to Buy and Sell Stock II的更多相关文章
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...
- 【LeetCode】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】【Medium】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 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 ...
- 【数组】Best Time to Buy and Sell Stock I/II
Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...
- 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 在windows 环境下对于 git 服务器的安装和使用
前言: 虽然说在团队开发的时候会有版本控制服务器,但是个人自己开发的时候,有的时候也需要有个版本控制下,比如,你改好了一个小的功能,然后在这个功能上继续扩展,结果扩展不成功,于是回到这个小功能上去.当 ...
- Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStr ...
- 【Matplotlib】设置刻度(1)
刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(*args, **kwargs) 获取或者设置当前刻度位置和文本的 ...
- maven 热部署成功案列
首先配置tomcat-user.xml,这个文件是在tomcat的conf文件夹下面 在</tomcat-users>前添加这段 <role rolename="admin ...
- junit加载
Run as junit 不会出现,把junit 包倒入lib文件夹中,在类的后面加上extends TestCase,此时上方会导入一个包:import junit.framework.TestCa ...
- JSP业务逻辑层
经典的三层架构:表示层.业务逻辑层和数据访问层 具体的区分方法 1:数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成各个对数据文件的操作.而不必管其他操作. 2:业务逻辑 ...
- iOS推送失败的可能问题汇总
ITC上的证书问题 AppID未开启推送 Provioning Profile在AppID开启推送功能前生成的 Provioning证书过期 推送的pem证书过期 客户端问题 target的CodeS ...
- abstract 类也可以继承 实体类
public class BaseReq { public String UserId { get; set; } public BaseReq() { } } public abstract cla ...
- Sublime的快捷键的使用
Default Keymap 1. Ctrl+L 选择整行(按住-继续选择下行) 2. Ctrl+Shift+K(shhift+del) 删除整行, ctrl + KK 从光标处删之行尾,Ctrl+K ...
- UVa OJ 180 - Eeny Meeny
Time limit: 3.000 seconds限时3.000秒 Problem问题 In darkest <name of continent/island deleted to preve ...