leetcode-easy-array-122 best time to buy and sell stocks II
mycode 69.45%
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
res = 0
for i in range(1,len(prices)):
temp = prices[i] - prices[i-1]
if temp <= 0:
continue
else:
res += temp
return res
参考:
下面的更快,因为索引查找的次数少一些!
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
else:
profit = 0
start = prices[0]
for i in range(1,len(prices)):
if prices[i]>start:
profit += prices[i] - start
start = prices[i]
else:
start = prices[i]
return profit
leetcode-easy-array-122 best time to buy and sell stocks II的更多相关文章
- [LeetCode&Python] Problem 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 al ...
- [Array]122. Best Time to Buy and Sell Stock II(obscure)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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 ...
- 【刷题-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 ...
- 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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- [转载]C / C++ 计算程序运行的时间
原文链接:https://blog.csdn.net/qq_36667170/article/details/79507547 在学数据结构过程中老师让查看不同算法的运行时间,然后让自己打印运行时间. ...
- python实现观察者模式
python实现观察者模式 前言 有时,我们希望在一个对象的状态改变时更新另外一组对象.在MVC模式中有这样一个非 常常见的例子,假设在两个视图(例如,一个饼图和一个电子表格)中使用同一个模型的数据, ...
- 运维日常错误总结(docker)
一:Apache服务启动失败 报错原因:80端口被占用 分析:netstat -anp|grep 80 检查80端口的占用情况,发现是启动了nginx服务,占用了http服务 解决方式: 1:如ngi ...
- 自动化监控软件之zabbix安装
自动化监控系统 cacti : 基于snmp(简单的网络管理协议)协议的监控软件,强大的绘图软件 缺点: 自带的监控模板比较少,不能默认 自带监控报警功能(只能自己去官网下载模板) Nagios: 插 ...
- 22_1mybaits入门
1.什么是框架? 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题. 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开发效率. 2.三层架构 表现层 ...
- Linux 安装 nginx 安装PCRE库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...
- JQuery 处理 微擎传递过去数据
JQuery 处理 微擎传递过去数据 PS:微擎得到的数据大多数是数组(我们这里处理数组) 将数组使用 json_encode() 函数处理成 JSON 格式 前端在 script 中使用 引号 将变 ...
- Spring + Mybatis 企业应用实战 第1章 Java EE应用
Java EE应用的分层模型: Domain Object 领域对象层.就是一些pojo. DAO(data access object) 数据访问对象 Service 业务逻辑层 Controlle ...
- Linux安装redis,启动配置不生效(指定启动加载配置文件)
一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...
- 关于 html button 点击刷新页面的问题
如果不想点击button 刷新页面的话,需要加个属性 type="button" 如下: <button class="layui-btn" type ...