题目:

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. 构建第一个Spring Boot2.0应用之项目创建(一)

     1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...

  2. ubuntu14.04安装gradle

    一.下载gradle $ wget https:////services.gradle.org/distributions/gradle-3.5.1-all.zip $ sudo unzip grad ...

  3. TestNG并发测试包

    https://www.yiibai.com/testng/basic-annotations.html

  4. 【HDU1542】Atlantis (扫描线的经典运用)

    点此看题面 大致题意: 给你\(N\)个矩形,请你求出它们覆盖的面积(重叠的面积只算一次). 扫描线 这道题是一道典型的求矩形面积并问题,是扫描线的一个经典运用.这里就不赘述了. 代码 #includ ...

  5. 关于profile集合

    profile集合是mongodb的慢操作日志 > db.getProfilingStatus() { , , } 可以通过getProfilingStatus来查看当前profile设置 pr ...

  6. 进入Windows之前发出警告

    实现效果: 知识运用: 通过注册表中HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\子键下的LegalNoticeCaption ...

  7. js处理的8种跨域方法

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  8. 题解 P1280 【尼克的任务】

    传送门 f[i]表示i~n的最长空闲时间: 如果当前无任务就休息一秒(f[i]=f[i+1]+1): 否则f[i]=max(f[i],f[i+当前工作时间]); 用结构体来记录,我们对于每一个时刻开一 ...

  9. 列表与特殊字符,div(新手HTMLL基础)

    1.无序列表 -项目符号:实心圆(disc).方框(square).空心圆(circle) -列表<ul>---- 列表项<li>--- </li></ul& ...

  10. 处理侧滑返回与 ScrollView 手势冲突

    与处理双击.单击手势互斥原则一样: // 手势互斥(侧滑返回手势失效后才响应UITableView的滑动手势) [tableView.panGestureRecognizer requireGestu ...