题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/

题目大意:给出一串数组,找到差值最大的差值是多少,要求只能用下标大的减下标小的,例子如下图:

法一(超时):直接两个for循环,进行一一比较,找出差值最大的点,但是超时了,所以这里后台应该规定的是1m的时间,而在1m的时间限制下,复杂度只有10^7左右,或者说不到10^8,这个题的有一个测试用例就超过了一万的数组大小,所以超时,代码如下:

                 int max = 0;
int length = prices.length;
for(int i = length - 1; i > 0; i--) {
for(int j = i - 1; j >= 0; j--) {
if(max < (prices[i] - prices[j])) {
max = prices[i] - prices[j];
}
}
}
return max;

法二(借鉴):贪心,一次遍历,从数组最左端开始每次都找相对较小的买入价,然后更新买入价,再找相对较大的卖出价,然后更新利润,代码如下:

         int min = Integer.MAX_VALUE;
int res = 0;
//第i天的价格可以看作买入价,也可以看作卖出价
for(int i = 0; i < prices.length; i++) {
//找到更低的买入价
if(prices[i] < min) {
//更新买入价
min = prices[i];
}
else if(prices[i] - min > res){
//更新利润
res = prices[i] - min;
}
}
return res;

121.Best Time to Buy and Sell Stock---dp的更多相关文章

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

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

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

  9. (Array)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 ...

  10. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

随机推荐

  1. BZOJ5016 Snoi2017一个简单的询问(莫队)

    容易想到区间转化成前缀和.这样每个询问有了二维坐标,莫队即可. #include<iostream> #include<cstdio> #include<cmath> ...

  2. 洛谷 P2055 [ZJOI2009]假期的宿舍

    洛谷 P2055 题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C ...

  3. 谈谈 Java 类加载机制

    概述 类加载器主要分为两类,一类是 JDK 默认提供的,一类是用户自定义的. JDK 默认提供三种类加载器: Bootstrap ClassLoader 启动类加载器:每次执行 java 命令时都会使 ...

  4. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. 安装lighttpd

    依赖包: zlib,pcre,cronolog,bzip2, 1: 将lighttpd的原码包.以土豆现用lighttpd配置文件为基础的lighttpd.conf文件.日志轮循工具cronolog  ...

  6. cmakelist 定义字符串,替换到脚本中。

    cmake_minimum_required(VERSION 2.6 FATAL_ERROR) cmake_policy(VERSION 2.6) # . Project Name project(s ...

  7. laravel query builder/ Eloquent builder 添加自定义方法

    上次干这事已经是一年前了,之前的做法特别的繁琐.冗余,具体就是创建一个自定义 Builder 类,继承自 Query\Builder,然后覆盖 Connection 里面获取 Builder 的方法, ...

  8. HDU3400 三分套三分

    题意 就是给你两条线段AB , CD ,一个人在AB以速度p跑,在CD上以q跑, 在其他地方跑速度是r.问你从A到D最少的时间. 三分AB ,然后再三分CD ,模板题目,这题卡精度 eps不能少 #i ...

  9. C#中调用Dll动态链接库

    C#中调用Dll动态链接库 起始 受限于语言的不同,我们有的时候可能会用别人提供的函数及方法 或者其他的什么原因.反正就是要调!!! 恰巧别人所使用的的语言跟自己又不是一样的 这个时候想要调用别人的函 ...

  10. selectors

    一.Selectors模块 它具有根据平台选出最佳的IO多路机制,比如在win的系统上他默认的是select模式而在linux上它默认的epoll,建议使用selectors. 常用共分为三种:sel ...