题目:

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

解题思路:

这题与上一题Best Time to Buy and Sell Stock相比,其实还更简单,扫描一遍数组,当前前元素大于前一个元素,则将其差值累加到max中,最后求得的max即为最大利益。

代码:

#include <iostream>
#include <vector> using namespace std; /**
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.empty())
return 0;
int max = 0;
for(int i = 1; i < prices.size(); i++)
{
if(prices[i] > prices[i-1])
max += prices[i] - prices[i-1];
}
return max;
}
}; int main(void)
{
int arr[] = {2,4,5,1,7,10};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> stock(arr, arr+n);
Solution solution;
int max = solution.maxProfit(stock);
cout<<max<<endl;
return 0;
return 0;
}

LeetCode122:Best Time to Buy and Sell Stock II的更多相关文章

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

  2. LeetCode122——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 a ...

  3. [LintCode] 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 ...

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

  5. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

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

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

  8. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  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. Node.js与Sails~Model数据模型

    回到目录 对于Sails来说,它的Model与数据库对应,不过它并没有采用目前比较流行的poco贫血模型,而是采用了类似DDD的充血模型,即它的数据实体里即有数据库字段(属性)而且还有方法,而模型里的 ...

  2. Atitit 数据存储的数据表连接attilax总结

    Atitit 数据存储的数据表连接attilax总结 1.1. 三种物理连接运算符:嵌套循环连接.合并连接以及哈希连接1 1.2. a.嵌套循环连接(nested loops join)1 1.3. ...

  3. cordova platform add specified version

    cordova platform add specified version 命令格式 cordova platform add android@4.0 可用的版本 Valid install tar ...

  4. 3D touch在Unity3D中的使用

    0.开篇: 3D touch随着iOS9发布,它并不是一个单独的技术,而是可以分为pressure sensitivity.quick action以及peek&pop.在官方的介绍中提到可以 ...

  5. Android线程之并发处理

    上一篇为大家介绍了关于Looper的简单知识,本篇我们介绍一下多线程的并发处理,我们知道Handler通过sendMessage()发送的消息,首先发送给了Looper,存入Looper的消息栈,之后 ...

  6. codeforces B. Pasha and String(贪心)

    题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai, 将字符序列区间为[ai,len-ai+1]进行反转.求出经过n次反转之后的序列! /* 思路1:将区间为偶数次的直接去掉!对 ...

  7. javaBean和jsp应用

    原网页:http://www.douban.com/note/102320977/ JavaBean是一种可复用,跨平台的组件.共有两种JavaBean:一种无用户界面,这种一般用于处理数据运算,操作 ...

  8. Java多线程系列--“JUC原子类”01之 框架

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4类. 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: AtomicIn ...

  9. hash算法总结收集

    hash算法的意义在于提供了一种快速存取数据的方法,它用一种算法建立键值与真实值之间的对应关系,(每一个真实值只能有一个键值,但是一个键值可以对应多个真实值),这样可以快速在数组等条件中里面存取数据. ...

  10. 01-Vue.js入门系列

    1.1. Vue简介 Vue是一个前端的双向绑定类的框架,发音[读音 /vjuː/, 类似于 view].新的Vue版本参考了React的部分设计,当然也有自己独特的地方,比如Vue的单文件组件开发方 ...