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

创建差分数组,即差分数组grid[i] = price[i+1]-price[i],因此,本题可以转化为求grid中最大子数组和问题

 public int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int minus[] = new int[prices.length-1];
for(int i=0;i<minus.length;i++){
minus[i] = prices[i+1]-prices[i];
}
int max = minus[0];
int now = minus[0];
for(int i=1;i<minus.length;i++){
if(minus[i]+now <minus[i]){
now = minus[i];
}else{
now = minus[i]+now;
}
if(max<now){
max = now;
}
}
return max>0?max:0;
}

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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

还是差分数组,把数组中所有正数加起来就是了。。。

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

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

动态规划,用两个数组记录状态,f[i]表示区间[0,i](0<=i<=n-1) 的最大利润,g[i]表示区间[i,n-1](0<=i<=n-1) 的最大利润。

 public int maxProfit(int[] prices) {
if (prices.length < 2)
return 0;
int f[] = new int[prices.length];
int g[] = new int[prices.length];
for (int i = 1, valley = prices[0]; i < prices.length; ++i) {
valley = Math.min(valley, prices[i]);
f[i] = Math.max(f[i - 1], prices[i] - valley);
}
for (int i = prices.length - 2, peak = prices[prices.length - 1]; i >= 0; --i) {
peak = Math.max(peak, prices[i]);
g[i] = Math.max(g[i], peak - prices[i]);
}
int max_profit = 0;
for (int i = 0; i < prices.length; ++i)
max_profit = Math.max(max_profit, f[i] + g[i]);
return max_profit;
}

Best Time to Buy and Sell Stock I II III的更多相关文章

  1. Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III

    题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

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

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

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

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

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

  7. [LintCode] 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 ...

  8. [LintCode] 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 ...

  9. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

  10. 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记

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

随机推荐

  1. 单路CPU性能排名 更新于2015.10.6

    http://itianti.sinaapp.com/index.php/cpu 排名 处理器 图例 分数 1 Intel Xeon E5-2699 v3 @ 2.30GHz 22892 2 Inte ...

  2. 【czy系列赛】czy的后宫4 && bzoj1925 [Sdoi2010]地精部落

    [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到-N之间的整数.他 ...

  3. c++ 08

    一.程序的错误 1.编码错误:编译阶段 2.设计错误:测试阶段 3.环境错误:使用阶段 4.应用错误:测试和使用阶段 二.错误处理机制 1.通过返回值处理错误 当一个函数在执行过程中发生了某种错误,通 ...

  4. UI设计(流程/界面)设计规范

    1.UI设计基本概念与流程 1.1 目的 规范公司UI设计流程,使UI设计师参与到产品设计整个环节中来,对产品的易用性进行全流程负责,使UI设计的流程规范化,保证UI设计流程的可操作性. 1.2范围  ...

  5. soj 1698 Hungry Cow_三角函数

    题目链接 题意:有只牛要吃草,现在有个墙挡着,给你绑着牛的绳的长度,墙的长度,绳原点到墙的距离,问牛能在多大的面积里吃草 思路:分为四种情况,详情请看书.被dp卡着这题没做成 #include < ...

  6. hdu 5676 ztr loves lucky numbers(dfs+离线)

    Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...

  7. ACM—Number Sequence(HDOJ1005)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f ...

  8. Promise 异步执行的同步操作

    Promise 是用来执行异步操作的. 但有时一个异步操作需要等其他的异步操作完成,这时候就可以使用then来做. function loadImageAsync(url) { return new ...

  9. 计算机图形学学习方法和相关书籍,做游戏,GIS,虚拟现实,三维引擎的都能够看看.

    本书參照<<图形学扫盲>> 整理的,原文内容引子: http://www.cppblog.com/lai3d/archive/2008/12/30/70796.html 前言: ...

  10. windows2008无线网卡和.net3.5安装

    今天在联想T420S笔记本上安装windows2008标准版,安装完成后部分驱动软件不能安装,要求.net framework3.5,下载.net3.5安装时提示应该用角色管理器安装. 根据提示打开服 ...