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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.

求最佳买入卖出时的收益。

暴力法,超时。

public class Solution {
public int maxProfit(int[] prices) {int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
for( int i = 0;i<len;i++){
for( int j = i+1;j<len;j++){
if( prices[j]>prices[i])
result = Math.max(result,prices[j]-prices[i]);
} }
return result;
}
}

所以思考一下这道题的原理,其实就设定一个指针就好了

设定刚开始的数字是买入价格:

1、如果高于买入价格,那么就和result相比,取较大的。

2、如果是低于价格,那么更新买入价格。

public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
int buy = prices[0];
for( int i = 1;i<len;i++){
if( prices[i] > buy )
{
result = Math.max(result,prices[i]-buy);
}else
buy = prices[i];
} return result; }
}

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

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

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

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

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

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

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

  8. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  9. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

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

随机推荐

  1. Jquery实现的Tabs标签页

    效果图: HTML: <div class="tabs"> <ul id="tabs"> <li class="tab- ...

  2. mybatis ForEach Collection集合等规范解析(转)

    转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...

  3. hdu 1950 最长上升子序列

    //Accepted 3540 KB 62 ms //dp 最长上升子序列 #include <cstdio> #include <cstring> #include < ...

  4. Ubuntu 14.10 下grep命令详解

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  5. 支撑向量机(SVM)

    转载自http://blog.csdn.net/passball/article/details/7661887,写的很好,虽然那人也是转了别人的做了整理(最原始文章来自http://www.blog ...

  6. RFIDler:一款定义RFID的读、写、仿真器的开源软件

    很多类似于RFID这样的技术看起来都很神秘,实际上他是依赖于很多物理学原理的,比如”电磁感应原理”.是的,这些现象产生的各种信号足以令人发狂,看完这些模拟模拟信号后,我忽然发现二进制信息多么干净美丽. ...

  7. 求一个数组中最小的K个数

    方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...

  8. Python ~~~ 面向对象的利器

    class Rectangle(): # 有没有括号都行 . def __init__(self,x,y): self.x=x self.y=y def getPeri(self): def getA ...

  9. iOS LaunchScreen启动图设置

    新建的iOS 项目启动画面默认为LaunchScreen.xib 如果想实现一张图片作为启动页,如下图 如果启动不行  记得clear 一下工程 是启动页停留一段时间  只需要在 AppDelegat ...

  10. Redirect HTTP to HTTPS on Tomcat

    I have bought my SSL secure certificate and successfully installed on Tomcat with the keytool but ho ...