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.

public class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int res=prices[1]-prices[0]; //开始是想用定义一个新的数组的,写到一半发现只要2个变量就好了
int curmin=(res<0?prices[1]:prices[0]);
for(int i=2;i<prices.length;i++){
res=Math.max(res,prices[i]-curmin);
curmin=Math.min(prices[i],curmin);
}
return (res>0)?res:0; //注意返回值肯定不是负数
}
}

  

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

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

  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 (Array;DP)

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

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

随机推荐

  1. linux sed的使用

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理, 可以将数据行进行替换.删除.新增.选取等特定工作. sed本质上是一个编辑器,但是它是非交互式的,这点与VIM不同:同时 ...

  2. 技海拾贝 - Android

    1. 前台Service - 介绍: http://blog.csdn.net/think_soft/article/details/7299438 - 代码实例:  http://blog.csdn ...

  3. 【科普】为什么WiFi自动信道选到的信道多数在1/6/11

    http://bbs.hiwifi.com/thread-4126-1-1.html 论坛上不少朋友很困惑,为什么小极的自动信道选择选到的信道只会在1.6.11这三个信道呢?WiFi不是一共有10几个 ...

  4. 5种io模式

    1)阻塞I/O(blocking I/O) 发起请求,等待数据报准备好,拷贝数据,数据返回指令 应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好. 如果数据没有准备好,一直等待….数据准备好 ...

  5. e

    frame问题 以下面的frame为例: ? 1 <frame  src=”xxx.html”  id=”frameId”  name=”frameName”  /> (1)访问frame ...

  6. Android驱动开发前的准备(二)

    搭建android开发环境 2.1 Android底层开发需要哪些工具 2.2 安装 JDK 2.3 搭建Android 应用程序开发环境 2.4安装Android NDK开发环境 2.5安装交叉编译 ...

  7. php 添加钩子实例

    <?php/*定义钩子函数*/function add($hook,$actionFunc){    global $emHooks;    if(isset($emHooks[$hook])) ...

  8. Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. 一些好的python IDE

    pyscipter 是一个不错的选择,快速灵巧.功能丰富.它的安装包只有五六兆,功能却一个都不少.语法高亮功能也很强,运算符.数字.hex都能按照你的需要改变颜色.还有非常灵敏的code comple ...

  10. NPOI读取Excel

    项目环境:Webform framework4.0 dll版本:NPOI2.0  dotnet2.0版本 这两天要做个excel导入的功能,想到以前用过NPOI,感觉很给力,今天写了个DEMO,写的时 ...