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. Hibernate中的多对多映射

    1.需求 项目与开发员工 一个项目,有多个开发人员 一个开发人员,参与多个项目 [多对多] 2.实体bean设计 Project: public class Project { private int ...

  2. java基础之 溢出

    堆溢出 堆(Heap)是Java存放对象实例的地方. 堆溢出可以分为以下两种情况,这两种情况都会抛出OutOfMemoryError:java heap space异常: 1.内存泄漏 内存泄漏是指对 ...

  3. MapReduce基础

    这篇文章翻译自Yahoo的Hadoop教程,很久之前就看过了,感觉还不错.最近想总结一下以前学的东西,看到现在关于Hadoop的中文资料还比较少,就有了把它翻译出来的想法,希望能帮助到初学者.这只是Y ...

  4. table td的宽度详解

    前言:一直总觉得td的宽度好难驾驭,但万事万物总是有规律的.就像亮剑说的:不用因为怕八路就敬而远之,应该靠上去,熟悉他们,了解他们.   正文:           Table只有Table的宽度是可 ...

  5. 嵌入式 -- WINKHUB 边信道攻击 (NAND Glitch)

    0x00 前言 随着物联网IOT的飞速发展,各类嵌入式设备, 路由器安全研究也越来越火. 但因为跟以往纯软件安全研究的要求不同, 这类研究往往需要结合相应的硬件知识. 很多朋友困惑如何开始, 甚至卡在 ...

  6. ACDream-C - Transformers' Mission(Dijastra最短路径)

    dijstra求最短路径:经典应用题目: 题意:给你一个带权值无向图,权值是A点到B点的时间,然后告诉你起点,一个人可以去炸掉一个结点或多个节点,也可以派多个人,最终这些人在终点集合,问最后一个到达终 ...

  7. ACE - 代码层次及Socket封装

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. ACE源码约10万行,是c++中非常大的一个网络编程代码库,包含了网络编程的边边角角.在实际使用时,并不是 ...

  8. 【转】Paxos算法深入分析

    http://blog.csdn.net/anderscloud/article/details/7175209 在分布式系统设计领域,Paxos可谓是最重要一致性的算法.Google的大牛们称   ...

  9. 重学STM32----(二)

    前几天买了个蓝牙模块,昨天到来了,就打算来研究研究蓝牙.看了蓝牙模块的资料,知道通讯需要串口,那肯定要先写一个串口程序了.要是用库函数写,10多分钟可能就会搞定,但是这就违背我的初衷了,所以就不知天高 ...

  10. git 恢复工作区删除的所有文件

    /********************************************************************* * git 恢复工作区删除的所有文件 * 说明: * 今天 ...