[LeetCode]题解(python):121-Best Time to Buy and Sell Stock
题目来源:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
题意分析:
给定一个数组,代表array[i] 代表第i天的价格。问买买卖这个物品一次的最高利润是多少(i买,j卖,j > i)。
题目思路:
记录当前最小值,如果array[i] < min,那么更新min,否者计算如果在i天的卖的利润,和当前最大利润比较。
代码(python):
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
ans,mins = 0,prices[0]
for i in prices:
if i > mins:
ans = max(ans,i - mins)
else:
mins = i
return ans
[LeetCode]题解(python):121-Best Time to Buy and Sell Stock的更多相关文章
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- LeetCode(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 ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
随机推荐
- 从零开始Unity3D游戏开发【1 常用快捷键】
Unity3D近两年起来的很快.自己对游戏一直很有兴趣,所以想找机会转游戏开发.目的之一是赚钱,目的之二是完成自己的小梦想. Unity3D的人才还是紧缺的,招聘网站上各个公司真敢拿着钱砸.让.NET ...
- HTML系列(五):超链接
<a> 标签定义超链接,最重要的属性是 href 属性,它指示链接的目标.通过将href属性设定不同类型的值可以使链接指向不同类型的链接地址:内部链接.外部链接.锚链接. 内部链接指的是同 ...
- s2sh遇到的问题
一:ids for this class must be manually assigned before calling save() "类名.hbm.xml"映射文件中< ...
- Oracle分析函数之开窗子句-即WINDOWING子句
Oracle的分析函数,对我们进行统计有很大的帮助,可以避免一些子查询等操作,在统计中,我们对开窗函数的接触较少,下面主要介绍下开窗函数的使用; http://www.itpub.net/thread ...
- 5分种让你了解javascript异步编程的前世今生,从onclick到await/async
javascript与异步编程 为了避免资源管理等复杂性的问题,javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是 ...
- 基于maven进行spring 和mybatis的整合(Myeclpise)
学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...
- [非技术参考]C# Socket网络编程
我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: 1. TCP/IP层次模型 当然这里我们只讨论重要的四层 01,应用层(Application):应用层是个很广泛的概念,有一 ...
- 建立一个ROS msg and srv
msg是一个描述ROS消息字段的简单的文本文件,它们经常用来为消息产生不同语言的源代码. srv文件描述一个服务,它由请求和响应两部分组成. msg文件被存储在一个包的msg目录下,srv文件被存储在 ...
- python 实现单链表
#! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures ...
- Hadoop 处理“Name node is in safe mode”问题(转)
运行hadoop程序时,有时候会报以下错误:org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Nam ...