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. sencha toucha获取 constructor中的数据

    config:{ tmp:null }, constructor : function(conf) { this.config.tmp=conf; } 添加配置属性,然后直接用 this.config ...

  2. DotnetBrowser高级教程-(4)使用MVC框架1-搭建基础框架

    我们这次重点介绍下MVC框架的使用. 如果您之前使用过微软的mvc web框架,那么dotnetbrowser的mvc框架相信您也会很快上手,两者之间相似度约有80%. 所谓的mvc,以一个例子为例: ...

  3. Linux内核中等待队列的几种用法

    Linux内核里的等待队列机制在做驱动开发时用的非常多,多用来实现阻塞式访问,下面简单总结了等待队列的四种用法,希望对读者有所帮助. 1. 睡眠等待某个条件发生(条件为假时睡眠): 睡眠方式:wait ...

  4. Java内存泄漏及分析

    对于内存泄漏,首先想到的是C语言,其实不然,java中也有各种的内存泄漏.对于java程序员,在虚拟即中,不需要为每一个新建对象去delete/free内存,不容易出现内存泄漏.但是,正 是由于这种机 ...

  5. TP框架模板中ifelse

    {if $_username}<ul class="afterLogin">    <li class="username"><a ...

  6. Vue框架引入JS库的正确姿势

    参考自:https://mp.weixin.qq.com/s?src=3&timestamp=1527154113&ver=1&signature=tWGeTa86gyK*RL ...

  7. 【电子签章】HTML格式合同转化成PDF文件

    代码地址如下:http://www.demodashi.com/demo/12476.html 一.主要思路 通过itextpdf 生成想要的pdf 文件 通过itextpdf 中 XMLWorker ...

  8. FileUpload控件预览图片

    HTML代码: <tr> <td class="auto-style1">上传图片:</td> <td> <asp:FileU ...

  9. iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客

    近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  10. Battery Charging Specification 1.2 中文详解

    转:       http://blog.csdn.net/liglei/article/details/228 1. Introduction 1.1 Scope 规范定义了设备通过USB端口充电的 ...