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.

解题思路:

本题目是《算法导论》 4.1 节 给出的股票问题的原题,可以转化为最大子数组的问题,书本上给出的是分治的做法,练习4.1-5给出了线性时间的算法。

Java for LeetCode 053 Maximum Subarray实现了相应的解法,如果不转化为最大子数组问题,解法如下:

一次遍历,每次找到最小的Buy点即可,JAVA实现如下:

    public int maxProfit(int[] prices) {
int buy = 0;
int profit = 0;
for (int i = 0; i < prices.length; ++i) {
if (prices[buy] > prices[i])
buy = i;
profit = Math.max(profit, prices[i] - prices[buy]);
}
return profit;
}

Java for LeetCode 121 Best Time to Buy and Sell Stock的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

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

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

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

  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. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

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

  8. Java for 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 al ...

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

随机推荐

  1. QQ聊天窗口上的广告与QQ弹窗广告的完美屏蔽去除

    涉及的软件 1. QQ  (笔者的为v8.2版本) 2. Win7 3. ADSafe(3.13.308.9900正式版) 前言 QQ广告十分讨人厌,除了QQ弹窗的广告,让人十分反感外,最近发现QQ聊 ...

  2. Centos下添加用户组

    近日,重新整理了下开发环境,重装了,nginx,但是这个时候却是报错了,报错信息如下: [root@hserver1 php-7.0.5]# nginx -t nginx: [emerg] getpw ...

  3. php根据日期时间生成随机编码

    订单.申请单之类的需要一个编码,通过下面方法可得 代码如下: $PNUM="PG" . date("YmdHis").rand(1000,9999);

  4. HTML5 Canvas 绘制星条旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  5. Android开发之布局文件里实现OnClick事件关联处理方法

    一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...

  6. mysql if()一个奇怪的问题

    看起来一切正常......... 但是当使用不等于时 SELECT IF(1!=NULL,1,2) 居然返回2 SELECT IF(1!=NULL,1,2) >2 -- ------------ ...

  7. apache的配置优化

    [APACHE的工作方式] prefork模式(默认)这个多路处理模块(MPM)实现了一个非线程型的.预派生的web服务器,它的工作方式类似于Apache 1.3.它适合于没有线程安全库,需要避免线程 ...

  8. spring 官方文档

    英文 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 翻译(1-6章) http:// ...

  9. Android · 获取网络图片

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  10. Android学习笔记(24):进度条组件ProgressBar及其子类

    ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...