Best Time to Buy and Sell sock II

时间:2023-01-03 13:21:39

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

分析:本题的意思就是说你同一时间只能拥有一只股票,在一段时间呢最低时买进,最高时卖出,赚得利润最多。相当于给你一组数组,例如{1,2,3,4,0,2,3,1},在刚开始1时买进,到4时,下一个数字为0了,4是这段时间的最高价,应卖出,然后再买进循环下去,把所得差额加起来就是最大利润了。

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

python实现:

class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
nLen=len(prices)
profit=
if nLen==:
return
elif nLen<=:
return
else:
for i in range(nLen-):
diff=prices[i+]-prices[i]
if diff>:
profit+=diff
return profit

Best Time to Buy and Sell sock II的更多相关文章

  1. &lbrack;LintCode&rsqb; 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 ...

  2. LEETCODE —— Best Time to Buy and Sell Stock II &lbrack;贪心算法&rsqb;

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  3. 27&period; Best Time to Buy and Sell Stock &amp&semi;&amp&semi; Best Time to Buy and Sell Stock II &amp&semi;&amp&semi; 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- ...

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

  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. 31&period; leetcode 122&period; 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 ...

  7. LeetCode&colon; 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 ...

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

  9. leetcode 121&period; Best Time to Buy and Sell Stock 、122&period;Best Time to Buy and Sell Stock II 、309&period; Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  2. web前端性能概述

    1.认识前端性能 不管你的网站设计的有多好,后端有多好,对于用户来说全部都是无感知的,用户只关心页面打开的速度,而前端性能表现很大程度上影响着用户的这种感知. 改善前端的性能对用户感知的整体性能提升有 ...

  3. jquery ui 改写cloes事件

    htmlAjax:{//模板ajax请求参数设置项            url:"template/task/task_create.html",            data ...

  4. flask 单个表单多个提交按钮

    单个表单多个提交按钮 在某些情况下,可能需要为一个表单添加多个提交按钮.比如在创建文章的表单中添加发布按钮和存草稿的按钮.当用户提交表单时,需要在视图函数中根据按下的按钮来做出不同的处理. 下面例子中 ...

  5. Confluence 6 设置一个空间主页

    当你创建一个空间的时候,Confluence 将会自动为你创建的空间新建一个主页.如果你的空间是从蓝图中创建,并且蓝图中已经有一个供使用的主页了,那么这个主页将会自动从蓝图中载入有用的宏和在蓝图中指定 ...

  6. Anaconda中python加入环境变量

    1.我的电脑---高级系统设置 2.选中环境变量,保存. 3.在系统环境变量PATH中,加入Anaconda3及Script路径加入其中 4.测试python

  7. linux gpio控制之sysfs接口

    在3.14及之后的linux中对gpio提供了sysfs接口,说明文档:Documents/gpio/sysfs.txt. Platforms which use the "gpiolib& ...

  8. javascript中原型&comma;构造器&comma;还有E5扩展的默认成员

    对象原型所具有的基本特征: 1.toString() 2.toLocaleString() 3.valueOf() 4.constructor() 5.propertyIsnumerable() 6. ...

  9. 【04】循序渐进学 docker:Dockerfile

    写在前面的话 从前面我们简单的了解了镜像,也运行了容器,各种官方的镜像显然无法满足我们自己的需求,我们目的终究是运行自己的业务. 所以,本章节的 Dockerfile 就主要讲怎么在官方镜像的基础上制 ...

  10. javascript测试框架mocha

    node测试框架mocha 简单.灵活.有趣,mocha是一个功能丰富的javascript测试框架,运行在node和浏览器中,使异步测试变得更加简单有趣.http://mochajs.org/ 安装 ...