翻译

话说你有一个数组,当中第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. 【转】Linux命令学习手册-split命令

    转自:http://blog.chinaunix.net/uid-9525959-id-3054325.html split [OPTION] [INPUT [PREFIX]] [功能]将文件分割成多 ...

  2. Git系列学习(1)-Git安装

    一.概述 msysGit名字前面的四个字面来源于MSYS项目: MSYS项目来源于MinGW(Minimalist GNU for Windows,最简GNU工具集) 通过添加一个bash提供的she ...

  3. jQuery中关于如何使用animate自定义动画

    动画 animate() 01.animate()方法的简单使用 有些复杂的动画通过之前学到的几个动画函数是不能够实现,这时候就是强大的animate方法了. 操作一个元素执行3秒的淡入动画,对比下一 ...

  4. C# 多线程系列(五)

    死锁 为了线程安全,我们在需要的是会使用”独占锁“,但过多的锁定也会有麻烦.多个线程因为竞争资源相互等待而造成的僵局,我们称为死锁.若无外力作用,这些进程将都无法推进.在死锁中,至少有两个线程被挂起, ...

  5. cmd 启动mysql环境变量配置

    win10系统:(其他系统类似,改环境变量就可以) 1.我的电脑,右键选择属性,进入系统页面 2.点击高级系统设置,进入系统属性页面 3.点击高级选项卡,点击环境变量,进入环境变量设置 4.选择系统变 ...

  6. ionic 创建某个文件下的page

    ionic g page 文件名 --pagesDir src/pages/about

  7. Windows如何正确的修改administrator用户名

    无论是服务器还是电脑.修改默认的administrator的用户总是好的.话不多少.直接上图 1.win+r  输入:gpedit.msc 2.按照我下图的圈圈找到重命名系统管理员账户并自定义 3.重 ...

  8. yum仓库配置ftpx协议

    [root@localhost ~]# iptables -F[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# syste ...

  9. loader__demo_css

    环境 node + yarn + webpack4.0 + webpack-cli + style-loader css-loader 文件结构 │ package.json │ webpack.co ...

  10. react 子组件给父组件传值

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...