Best Time to Buy and Sell Stock II
2024-09-23 22:41:44
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) {
if(prices.empty())
return ;
int price = prices[];
int profit=;
for(auto it = prices.begin();it!=prices.end();++it)
{
if(price<*it)
profit += *it - price;
price = *it;
}
return profit;
}
};
和最大子序列思路差不多。这里是前一天和后一天比较,如果前一天价格小于后一天就先买后卖,否则这一天不交易。
最新文章
- iOS开发之单元测试
- 记录HttpWebRequest辅助类
- html基本的内容
- 关于Repository模式
- webgame设计之功能模块的代理模式
- 使用Spring的Property文件存储测试数据 - 添加测试数据
- TextView------文字底部或者中间加横线
- java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)
- 【原】Storm环境搭建
- springmvc获得项目根目录(绝对路径)
- Crazy-Links
- 汽车Vin码识别——&#160;一款二手车行业值得拥有的OCR识别软件
- java面向对象浅析
- CodeSmith系列(三)——使用CodeSmith生成ASP.NET页面
- ZOJ 1002 DFS
- git 一些实用的api
- Python开发——2.基本数据类型之数字和字符串
- Linux下绑定网卡的操作记录
- CH 0101 - a^b / CH 0102 - 64位整数乘法 - [快速幂和快速乘]
- Ollydbg