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

这一题算是第一题的基础上稍微增加一点难度吧 数组的含义与我上文提到的一致 只是说他不限制你只能买一次了 你可以买任意次  只是在你再次买之前你必须先卖掉你之前买的东西  最终目的还是求最大利润。

这里其实理解了题目的话 解题感觉不难

可以画一个折线图 这道题的意思就是把所有上坡的地方加起来 粗糙的画个图

每一个红色圈住的地方代表一个小上坡 都已加进去

有了思路 码代码

public class Solution {
public int maxProfit(int[] prices) {
int profit=0;
for(int i=0;i<prices.length-1;i++){
int tmp=prices[i+1]-prices[i];
if(tmp>0){
profit+=tmp;
}
}
return profit;
}
}

  一次过 哦也

空间复杂度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. 转:MyBean简介

                        (在开始之前,非常感谢 D10.天地弦) 1.1 概述 MyBean是一个用于Delphi应用程序开发的开源.轻量级.可配置插件框架.它通过巧妙的系统架构设计, ...

  2. Ubuntu 12.04搭建l2tp服务器记录。

    1. 安装openswan apt-get install openswan 2.打开 /etc/ipsec.conf 文件,做如下配置: 其中,virtual_privat这里包含的网络地址允许配置 ...

  3. 通过浏览器https能够访问SVN,但eclipse SVN,tortoiseSVN始终连接不上SVN的问题解决方案

    为了便于本地代码维护,特意在本地搭建了一个visualSVN服务器用于本地代码管理,但是最近突然出现问题,eclipse上的SVN资源库始终连接不上,提示 "svn: connection ...

  4. CentOS+Apache+mod_wsgi+Python+Django

    前言 网上有关的教程千篇一律,都是无脑抄,自己都不验证一遍就直接复制,毫无意义,我通过官方文档和自己摸索,总结了一套教程. Django自带Web服务功能,但那只是方便开发调试,生产环境中一般将Dja ...

  5. awk实现join

    awk 有时确实比较省事,做些简单的文本处理,还是很方便的,在这介绍下两个文件的join的操作. 原始文本 bb.txt a 10 b 12 cc.txt a 11 b 13 c 15 awk joi ...

  6. iOS图片加载到内存中占用内存情况

    我的测试结果: 图片占用内存   图片尺寸           .png文件大小 1MB              512*512          316KB 4MB              10 ...

  7. Python 3.6.0的sqlite3模块无法执行VACUUM语句

    Python 3.6.0的sqlite3模块存在一个bug(见issue 29003),无法执行VACUUM语句. 一执行就出现异常: Traceback (most recent call last ...

  8. opengl

    基于OpenGL ES的GLfixed类型使用 OpenGL ES中引入了GLfixed类型.这个类型一般被定义为int,32位.高16位表示整数部分,低16位表示小数部分.由于其整数部分和小数部分所 ...

  9. linux下mv命令使用方法

    1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或目录 目标 ...

  10. Python常用模块学习

    1.模块介绍 2.time & datetime模块 3.random 4.os 5.sys 6.shutil 7.json&pickle 8.shelve 9.xml处理 10.ya ...