一个系列三道题,我都不会做,google之答案。过了两道,第三道看不懂,放置,稍后继续。

一、Best Time to Buy and Sell Stock I

题目:一个数组表示一支股票的价格变换。要求只买卖一次,获得最大收益。

思路:一开始我认为是寻找最大、最小值,但由于最大值不一定总是出现在最小值的后面,因此WA。

参考思路:DP。对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比。

代码:

 public int maxProfit(int[] prices) {
int len = prices.length ;
if(len < 2) return 0; int min = prices[0] ;
int maxProfit = 0; for(int i = 1 ; i < len ; i++){
int temp = prices[i] - min; //当前值减去前i-1个值的最小值
if(maxProfit < temp) maxProfit = temp; //更新最大收益
if(prices[i] < min) min = prices[i]; //看是否需要更新前i个值的min值,用于下次循环
} return maxProfit;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二、Best Time to Buy and Sell Stock II

题目:在上一题的基础上,允许对一支股票任意次买卖,(同一时间可先卖出再马上买入),同样求最大收益。

思路:如果第i个价格大于第i-1个价格,则将此部分收益加入到最大收益中,因为可以在第i个价格处马上卖出再马上买入。

代码:

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

三、待续

[leetcode]_Best Time to Buy and Sell Stock I && II的更多相关文章

  1. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  2. Leetcode: Best Time to Buy and Sell Stock I, II

    思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...

  3. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

  4. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  5. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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

  6. [LeetCode] 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 ...

  7. [LeetCode] 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 —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  9. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

随机推荐

  1. RDLC添加页码

    页脚显示页码并总页数首先添加页脚,在页脚拖一textbox,textbox的表达式输入:="第" & Globals!PageNumber & "页 共& ...

  2. VisualVM连接远程Java进程

    jstatd是一个RMI(Remove Method Invocation)的server应用,用于监控jvm的创建和结束,并且提供接口让监控工具(如VisualVM)可以远程连接到本机的jvms . ...

  3. 使用mustache.js 模板引擎输出html

    看了https://mustache.github.io/你就知道mustache是非常强大的模板引擎,支持多种语言,下面是个简单入门例子: MVC Model public class Studen ...

  4. jQuery:find()及children()的区别

    1:children及find方法都用是用来获得element的子elements的,两者都不会返回 text node,就像大多数的jQuery方法一样. 2:children方法获得的仅仅是元素一 ...

  5. linux下USB转串口驱动的安装

    ubuntu10.04,usb串口用的是moxa 1110 搞了半天没有驱动... 去官方下了个:http://www.moxa.com/support/sarch_result.aspx?type= ...

  6. Spring配置事务 http://www.cnblogs.com/leiOOlei/p/3725911.html

    http://www.cnblogs.com/leiOOlei/p/3725911.html JNDI方式配置数据源: <?xml version="1.0" encodin ...

  7. Hive静态分区表&动态分区表

    静态分区表: 一级分区表: CREATE TABLE order_created_partition ( orderNumber STRING , event_time STRING ) PARTIT ...

  8. 翻译「C++ Rvalue References Explained」C++右值引用详解 Part4:强制Move语义

    本文为第四部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/4220233.html. 强制Move语义 众所周知,正如C++标准的第一修正案所陈述:“委 ...

  9. uLua Unity工作机制

    基于ulua 1.25版本,开启C#类型动态注册. 一.  步骤 注册需要Wrap的C#类型. 在WrapFile.cs类中,使用_GT(typeof(XXX)), 注册需要Wrap的C#类型 注册的 ...

  10. C# ref和out的区别

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...