leetcode 123. Best Time to Buy and Sell Stock III ----- java
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
之前题的扩展,要求是只能买卖两次,然后求出最大利润。
利用动态规划,从前向后和从后向前各求一次,然后求出结果。
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2 )
return 0;
int[] preProfit = new int[len];
int[] maxProfit = new int[len];
int cur = prices[0];
preProfit[0] = 0;
for( int i = 1;i<len;i++){
cur = Math.min(cur,prices[i]);
preProfit[i] = Math.max(preProfit[i-1],prices[i] - cur);
}
cur = prices[len-1];
maxProfit[len-1] = 0;
for( int i = len-2;i>=0;i--){
cur = Math.max(cur,prices[i]);
maxProfit[i] = Math.max(maxProfit[i+1],cur - prices[i]);
}
int result = 0;
for( int i = 0;i<len;i++){
result = Math.max(preProfit[i]+maxProfit[i],result);
}
return result;
}
}
leetcode 123. Best Time to Buy and Sell Stock III ----- java的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [leetcode]123. 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 ...
- 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 ...
- LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Leetcode#123 Best Time to Buy and Sell Stock III
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- JNI与NDK简介
最近稍微了解一下JNI和NDK. 网上各种教程给人一种二者不分的感觉, 经过自己运行代码, 将两者的关系理了一下. 就目前了解,JNI应该是java自带的一种调用c和c++等语言(native cod ...
- Oracle Database 11g express edition
commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...
- C++指针详解(二)
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...
- RAD,V模型
介绍: RAD(Rap Application Developmen快速应用开发t)模型是软件开发过程中的一个重要模型,由于模型构图类似字母V,所以又称为软件开发的V模型.它通过开发和测试同时进行的方 ...
- MySQL的高级查询
高级查询 1.连接查询(对列的扩展) 第一种形式select * from Info,Nation #会形成笛卡尔积 select * from Info,Nation where Info.Nati ...
- 有关WAMPSERVER 环境搭建 如何修改端口,MySQL数据库的修改
环境搭建 http://share.weiyun.com/88896747fedd4e8b19afebea18f7684c 一.修改Apache的监听端口 1.在界面中选Apache,弹出隐藏菜单选项 ...
- declare 关键字在Oracle中的应用。
一般用在trigger或匿名存储过程中使用.如 declare a number;begina:=1;end;
- Python学习路程day7
多态 class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self ...
- Chapter 2: A Simple Servlet Container
一.这一章从头构建一个简单的Servlet容器,可以处理Servlet和静态资源(如html文件/图片等). 要处理Servlet,必须遵循javax.servlet.Servlet规范,而处理静态资 ...
- 谈谈 Mifare Classic 破解
2008 年的时候,荷兰恩智浦(NXP)公司开发的 RFID 产品 Mifare Classic 就被破解了,黑历史在这里就不在具体说了,想详细了解可以自己 Google 百度.现在还是重点说说关于 ...