一个系列三道题,我都不会做,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. java学习_文件工具类

    工具类里面的方法全部都是静态的,调用的时候不需要实例化

  2. 朗逸2011款 1.4t 清除保养告警灯

    朗逸2011款 1.4t 清除保养告警灯 Posted on 2015-03-01 21:06 编辑 仪表盘上有两个按钮 按住右边set键,钥匙旋转到通电状态,保持2s. 放掉set,按左边的切换按钮 ...

  3. 查看maven项目的依赖关系 mvn dependency:tree

    maven-dependency-plugin最大的用途是帮助分析项目依赖,dependency:list能够列出项目最终解析到的依赖列表,dependency:tree能进一步的描绘项目依赖树,de ...

  4. .NET基础操作回顾_使用ADO.NET操作SqlServer使用的类

    有些工具用的久了或者有新工具出现后,就慢慢的遗忘了很多,它们从熟悉的变成陌生,当然,对于我们来说不是好事吧. 今天回顾一下ADO.NET用到的MS的基础类库,先上代码(标准的SqlServer操作) ...

  5. 一个简单的AJAX实例

    创建一个简单的XMLHttpRequest,从一个TXT文件中返回数据. 来源于菜鸟教程 <!DOCTYPE html><html><head><meta c ...

  6. 队列理论和队列网络模型 queueing theory and queueing network model

    1队列理论 1.1队列在生活中随处可见,例如排队买票,排队打饭,排队做地铁等等.那将诸如此类的队列抽象一下,可归纳为一下5要术: 到达过程arrival process 服务时间的分布 service ...

  7. 在探索中感悟,在摸索中前进--ET之快递法

    飞测说:最近很多朋友问如果没有需求文档,我该怎么测试?我自然想起了探索测试,今天来说说自己在探索测试路上的一点点感悟-------快递测试法,让我们在探索中感悟,在摸索中前进-- 快递测试法,是从ET ...

  8. Gradle用户指南

    下载安装gradle 2.1 下载地址:http://www.gradle.org/learn 安装先决条件:gradle安装需要1.6或者更高版本的jdk(jre)(可以使用java –versio ...

  9. Guid.NewGuid().ToString()的几种格式

    1.Guid.NewGuid().ToString("N") 结果为:      38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid() ...

  10. Oracle 查看表空间剩余与创建空间语法

    select a.tablespace_name,total,free,total-free used from ( select tablespace_name,sum(bytes)/1024/10 ...