题目来源:

  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的更多相关文章

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

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

  3. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

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

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

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

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

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

  10. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

随机推荐

  1. Day01

    1.@Test函数,执行后控制台没有输出结果? 1)  不能用静态方法,控制台会没有结果. 2)  不能把类名命名为Test,@Test不识别. 2.遍历Map集合的entrySet方法不会? 3.使 ...

  2. android UI进阶之style和theme的使用

    今天来和大家分享一下android中UI设计里面常会用到的style和theme. 首先,style和theme都是资源,android提供了很多这样的默认资源.你可以来使用它们.同时你也可以自己定义 ...

  3. Android getReadableDatabase() 和 getWritableDatabase()

    Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...

  4. oracle表设置主键自增长

    create or replace table TBL_SYS_USER (   user_id             NUMBER(19) not null,   user_name        ...

  5. Python操作Access数据库

    我们在这篇文章中公分了五个步骤详细分析了Python操作Access数据库的相关方法,希望可以给又需要的朋友们带来一些帮助. AD: Python编 程语言的出现,带给开发人员非常大的好处.我们可以利 ...

  6. C#实现按Word模板导出Word(加书签bookMark)

    本方法是针对word导出操作,需要制作好的模板文件 模板.doc 引入应用Microsoft.Office.Interop.Word 11.0  (office2003) 导出文件注意:有时候迅雷会在 ...

  7. gridview中使用href调用javascript

    传递参数(多个)可用以下两种方法: 方法一: <asp:TemplateField HeaderText="列名1"> <ItemTemplate> < ...

  8. 2014.9.23window对象

    一.window对象 Wondow.navigate(url); 跳转页面(与超链接的区别:可以加逻辑条件) Var a = Math.random(); 0-1之间随机数 Var a = parse ...

  9. 通过springmvc的RequestMapping的headers属性的使用

    直接上图: springmvc中可以通过@RequestMapping注解折配置headers属性,也就是通过headers属性来配置请求头信息,从而通过这个属性值来映射请求,因为不同浏览器的Acce ...

  10. 随机数、continue、break

    arc4random() — 返回一个随机数(无符号整型).  如果要随机一个 [a, b]范围内的整数  公式:arc4random() % (b - a + 1) + a; #include &l ...