LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译
话说你有一个数组,当中第i个元素表示第i天的股票价格。
设计一个算法以找到最大利润。
你能够尽可能多的进行交易(比如。多次买入卖出股票)。
然而,你不能在同一时间来多次交易。
(比如。你必须在下一次买入前卖出)。
原文
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).
分析
事实上非常easy的一道题嘛。一開始想复杂了……
举了栗子:
// 4 7 8 2 8
最大利润非常明显是 (8 - 4) + (8 - 2) = 10
就由于这个式子让我想复杂了:首先要找到一个极小值4。然后找到极大值8。然后找到极小值2。然后找到极大值8;balabala……
事实上换一种思路,(7 - 4) + (8 - 7) + (8 - 2)
差别在于。直接将后一个数减前一个数就好了呀,仅仅只是假设后一个数比前一个数小的话就不考虑而已。
代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
size_t size = prices.size();
if (size <= 1) return 0;
int max = 0;
for (int i = 1; i < size; ++i)
max += prices[i] > prices[i - 1] ? prices[i] - prices[i - 1] : 0;
return max;
}
};
LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)的更多相关文章
- [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] 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 ...
- 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 ----- java
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 (stock problem)
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 ...
- 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 ...
随机推荐
- C#使用Parallel处理数据同步写入Datatable并使用BulkInsert批量导入数据库
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL). 业务流程:选择照片文件夹,分别访问照片-->调用DLL接口传递照片路径-->接收处理返回值-->写入数据 ...
- python自动化测试学习笔记-5常用模块
上一次学习了os模块,sys模块,json模块,random模块,string模块,time模块,hashlib模块,今天继续学习以下的常用模块: 1.datetime模块 2.pymysql模块(3 ...
- hibernate annotation 之 一对多、多对一双向外键关联
假设,一个农场产出多种植物,具体的某一植物产于某一农场. 3 import java.io.Serializable; 4 import java.util.Set; 5 import javax.p ...
- mongodb分片集群安装教程
mongodb 集群包含副本集群,主从集群以及分片集群,分片集群比较复杂,这里测试我采用了三台机器,交差部署 blog地址:http://www.cnblogs.com/caoguo 一 .环境:#m ...
- Split()函数
最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组. 贴出一个奇怪的例子 using System; using System.Collecti ...
- PAT_A1107#Social Clusters
Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...
- sysbench_fileio.sh
当我--file-total-size=30G 并且指定3个文件的时候,结果是产生3个10G的文件,然而--max-time=20被忽视了,虽然指定了20s的限制,实际上是在prepare阶段,--m ...
- git对vue项目进行版本管理
生成本地仓库 步骤一:git init 步骤二:git add * 步骤三:git commit -m 'init team' 创建远程仓库 new responstory 复制关联代码的命令 将本地 ...
- Scrapy Item用法示例(保存item到MySQL数据库,MongoDB数据库,使用官方组件下载图片)
需要学习的地方: 保存item到MySQL数据库,MongoDB数据库,下载图片 1.爬虫文件images.py # -*- coding: utf-8 -*- from scrapy import ...
- Linux之iptables(四、网络防火墙及NAT)
网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...