翻译

话说你有一个数组,当中第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)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Windows(7/8/10)搭建kibana 6.x版本(elasticsearch的可视化服务)

    在搭建kibana之前,我们先了解下什么是kibana Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作.您可以 ...

  2. mysql在linux下的安装(5.7版本以后)

    1.添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组. ①groupadd mysql ②useradd -r -g mysql mysql 2.将二进制文件解压到指定的 ...

  3. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  4. 【Leetcode】115. Distinct Subsequences

    Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...

  5. phpstorm如何在同一个文件夹打开多个目录

    phpstorm默认一个窗口只显示一个项目,如果新建一个项目,他会给你个选项卡,问你是在新窗口打开新项目还是在本窗口打开. 能不能在一个窗口打开多个项目呢?就像sublime text那样,其实是可以 ...

  6. Android HTTP 数据提交

    在Android 项目中,使用HTTP协议获取数据或者处理数据,需要使用到多线程和配置相应的APP权限 1.使用线程,使用HTTP 提交数据 private Thread submitThread = ...

  7. [Java]Java分层概念

      service是业务层 action层即作为控制器 DAO (Data Access Object) 数据访问 1.JAVA中Action层, Service层 ,modle层 和 Dao层的功能 ...

  8. 四次挥手与tcp标志位

    鉴于tcp的标志位可以同时置位,在相应端无数据传输时,四次握手可以用三次报文完成.

  9. CAD绘制一个直径标注(com接口VB语言)

    主要用到函数说明: _DMxDrawX::DrawDimDiametric 绘制一个直径标注.详细说明如下: 参数 说明 DOUBLE dChordPointX 在被标注的曲线上的第一个点X值 DOU ...

  10. CPU重要性能参数

    内容来自http://www.360doc.com/content/18/1124/15/60810319_796935567.shtml CPU有几个重要的参数:主频.核心.线程.缓存.架构.那么他 ...