题目:

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.size()== ) return ;
int sum_profits = , pre_price = prices[];
for ( size_t i = ; i < prices.size(); ++i )
{
sum_profits += (prices[i]>pre_price) ? prices[i]-pre_price : ;
pre_price = prices[i];
}
return sum_profits;
}
};

tips:

Greedy算法。

核心算法:如果当天的价格比前一天高,sum_profits就累加上当天的利润与前一天利润的差值(即昨天买,今天买);如果当天的价格比昨天低,则不更新sum_profits(即今天买,今天卖)。

========================================

第二次过这道题,低买高卖。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int ret = ;
for ( int i=; i<prices.size(); ++i )
{
if ( prices[i]>prices[i-] )
{
ret += prices[i]-prices[i-];
}
}
return ret;
}
};

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

  1. leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  2. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  3. leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  4. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

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

  6. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 【刷题-LeetCode】122 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 ...

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

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

随机推荐

  1. 根据accept-language自动设置UICulture和Culture

    在web.config中添加如下配置: <system.web> <globalization uiCulture="auto" culture="au ...

  2. pat甲级1139

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  3. linux 命令——27 chmod

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法. 一种是包含字母和操作符表达式的文字设定法: 另一种是包含数字的数字设定法. Linux系统中 ...

  4. Java AES加密算法工具类

    AESCodec.java package util; import java.security.Key; import javax.crypto.Cipher; import javax.crypt ...

  5. 常量池与方法区以及又读new String对象创建问题

    又拿出这道String str1 = new String("abc");创建几个对象的面试题梳理了一下常量池与方法区的关系,希望能把这两者的关系通过这道面试题说明白 方法区是什么 ...

  6. 在主机端和设备端进行”incrementArray“并对结果进行比较

    实验思想: 在主机端将数据初始化后传输到设备端,设备端和主机端进行同样的操作对数据加1,然后将设备端的结果传输到主机,最后核对主机端的计算结果和设备端的计算结果是否一直. // incrementAr ...

  7. Java设计模式学习——简单工厂

    一. 定义与类型 定义:有工程对象决定创建出哪一种产品类的实例 类型:创建型,但不属于GOF23中设计模式 二. 适用场景 工厂类负责创建的对象比较少 客户端(应用层)只知道传入工厂类的参数,对于如何 ...

  8. java基础必备单词讲解 day one

    computer 电脑 computer path 路径 配置jdk环境 class 类 classpath 类路径 编译好的文件执行路径 public 公共的 private 私有的 static ...

  9. Vue源码学习一 ———— Vue项目目录

    Vue 目录结构 可以在 github 上通过这款 Chrome 插件 octotree 查看Vue的文件目录.也可以克隆到本地.. Vue 是如何规划目录的 scripts ------------ ...

  10. C++的新特性for-each

    C++实验课要求用for each 循环来实现关联容器 map 的输出,一开始完全萌比.查了好久的资料才整理出下面的: C++11新特性之一就是类似java的for each循环: map<in ...