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 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. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. int profit=0;
  4. for(int i=0;i<prices.length-1;i++){
  5. int tmp=prices[i+1]-prices[i];
  6. if(tmp>0){
  7. profit+=tmp;
  8. }
  9. }
  10. return profit;
  11. }
  12. }

  一次过 哦也

空间复杂度o(1)级别  遍历一次时间复杂度0(n)  直接就是最优解了 不用看discuss了 继续看下一题

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记的更多相关文章

  1. 188. Best Time to Buy and Sell Stock IV leetcode解题笔记

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

  2. 122. Best Time to Buy and Sell Stock II ——LeetCode

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

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

  4. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

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

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

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

  6. 122. 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 an al ...

  7. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

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

  9. 【刷题-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 ...

随机推荐

  1. git入门札记

    分布式版本控制(个人主机即版本库,有一台作为“中央服务器”来方便“交换”修改,管理修改 而非文件) vs.  SVN CVS git 安装后设置: git config - -global user. ...

  2. ASP.NET 表单验证实现浅析

    首先,自然是配置 Web.config,在 <system.web> 下设定: <authentication mode="Forms"> <form ...

  3. ConnectionString属性尚未初始化

    问题前因:使用动软代码生成的三成模板然后复制到相应的类库 动软生成的 sql帮助类 推荐的是DBsqlhelp 期间引用了:BLl层:Maticsoft.Common.dll DAl层:Maticso ...

  4. win10使用技巧

    无法使用内置的管理员账户打开应用的问题命令行里输入:secpol.msc安全设置-本地策略-安全选项点击找到“用户账户控制:用于内置管理员账户的管理员批准”选项.该选项设置为"已启用&quo ...

  5. javascript算法

    代码运行环境: nodejs + mochajs /* *选择排序 *每次查找数组最小数据 *将最小数据排到左侧 */ var assert = require('assert'); describe ...

  6. Python全栈开发day4

    1.python三元运算 实现方法: result = 值1 if 条件 else 值2 例如: 1 2 3 4 #!/bin/env python #-*- coding:utf-8 -*- nam ...

  7. C++ MFC打开文件的流程

    打开文件的步骤如下: 弹出打开文件对话框 -> 获取选择的文件,并将文件显示在视图中. 我们程序中经常需要定制的操作如下: 1. 定制弹出的文件对话框,例如需要修改打开文件的类型或扩展名 2. ...

  8. C#删除文件

    string file =System.Web.HttpContext.Current.Server.MapPath(fileUrl); if (System.IO.File.Exists(file) ...

  9. dma驱动

    http://www.crifan.com/files/doc/docbook/dma_pl08x_analysis/release/html/dma_pl08x_analysis.html#idp2 ...

  10. Linux 常用命令 :cd命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...