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 size = prices.size();
int maxP = ;
for (int i = ; i < size; ++i) {
if (prices[i] > prices[i-])
maxP += prices[i] - prices[i-];
}
return maxP;
}
};

【Leetcode】【Medium】Best Time to Buy and Sell Stock II的更多相关文章

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

  2. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  3. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  4. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  5. [LeetCode] 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 ...

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

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

  8. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...

  9. 【leetcode刷题笔记】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. SAP Sybase IQ 操作基础

    1.启动 source IQ-16_0.sh 命令行查看安装程序是否成功 start_iq -v2 2.数据库.表空间 start_iq -n utility_db dbisql -c 'uid=db ...

  2. Zookeeper选举算法原理

    Zookeeper选举算法原理 Leader选举 Leader选举是保证分布式数据一致性的关键所在.当Zookeeper集群中的一台服务器出现以下两种情况之一时,需要进入Leader选举. (1) 服 ...

  3. ViewData与ViewBag的区别

    本文导读:在asp.net mvc程序设计中,传递数据常常会用到viewdata.viewbag.ViewData是一个字典集合,通过key值读取对应的value:ViewBag是动态类型,作用和Vi ...

  4. JDBC(5)-处理大数据

    大数据对象处理主要有CLOB(character large object) 和BLOB(binary large object) 两种类型的字段. 在CLOB中可以存储大字符对象,比如长篇小说:在B ...

  5. 获取两个数之间的随机数-java

    start=25 end=30 (int)(Math.random()*(end-start)+start)

  6. 【Lua】LWT遍历指定目录并输出到页面中

    首先用lua遍历目录: function getDirs(path) local s = {} function attrdir(p) for file in lfs.dir(p) do if fil ...

  7. STM32F407 使用HAL库延时微妙实现方法(附CubeMX配置过程)

    STM32F407 使用HAL库延时微妙实现方法(STM32CubeMX配置) 作者 : 李剀出处 : https://www.cnblogs.com/kevin-nancy/p/10696681.h ...

  8. unity优化

    1. 更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 2. 对于透明贴图,我们只能选择RGBA 16 ...

  9. mysql linux下表名忽略大小写注意事项

    在Unix中使用lower_case_tables_name=0,在Windows中使用lower_case_tables_name=2.这样了可以保留数据库名和表名的大小写.不利之处是必须确保在Wi ...

  10. log4j DailyRollingFileAppender, DatePattern 配置

    在DailyRollingFileAppender中可以指定monthly(每月). weekly(每周).daily(每天).half-daily(每半天).hourly(每小时)和minutely ...