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.

买卖股票,只能买卖一次。那么只需要简单遍历一遍,记录利润值和买入值,每次遇到更大的利润值就更新,遇到更小的买入值就更新。这样在每个day i处计算出的利润值为在第i天卖出所能得到的最大利润。不断更新这个利润,最后得到的即为最大利润值。

     public int maxProfit(int[] prices) {
if(prices.length<=0)
return 0;
int buy = prices[0];
int benifit = 0;
for(int i=0;i<prices.length;i++) {
benifit = Math.max(benifit, prices[i]-buy);
buy = Math.min(buy, prices[i]);
}
return benifit;
}

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) {
int re = 0;
for(int i=1;i<prices.length;i++) {
if(prices[i]>prices[i-1])
re += prices[i]-prices[i-1];
}
return re;
}

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

discussion里有人提出了一个dp方法适用于k次买卖的情况,很好理解。这里就直接照搬他的思路了:

// f[k, ii] 表示直到 prices[ii] 的最大利润 在最多k次交易的情况下.

// 转移函数:f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] } = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))

// 基本情况:f[0, ii] = 0; 0次交易将无利润

// 基本情况:f[k, 0] = 0; 如果只有一天也将无利润

     public int maxProfit(int[] prices) {
if(prices.length<=1)
return 0;
int k=2;
int[][] dp = new int[k+1][prices.length];
int re = 0;
for(int i=1;i<=k;i++) {
int temp = dp[i-1][0]-prices[0];
for(int j=1;j<prices.length;j++) {
temp = Math.max(temp, dp[i-1][j]-prices[j]);
dp[i][j] = Math.max(dp[i][j-1], prices[j]+temp);
}
}
return dp[k][prices.length-1];
}

[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III的更多相关文章

  1. [LeetCode][Java] 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 a ...

  2. [LeetCOde][Java] 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 a ...

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

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  5. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

  6. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  7. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  8. Best Time to Buy and Sell Stock I II III

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

  9. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

随机推荐

  1. myeclipse搭建SSH框架

    搭建SSH框架 Struts+hibernater+spring架构(myeclipse) 右击,首先加入spring,加入hibernater,再加入struts2 复制jar包(把tomcat发布 ...

  2. 记一个PowerShell的方法调用 --ResolveWindowsPrincipal

    没时间系统的学习PowerShell, 只能现学现用. 这段函数调用花了我半个多小时才搞定. 呵呵. 您别笑我, 呵呵. 在这里个例子里, 包括了PowerShell里如下的一些要点: 静态函数的调用 ...

  3. javascript的replace+正则 实现ES6的字符串模版

    采用拼接字符串的形式,将 JSON 数据嵌入 HTML 中.开始时代码量较少,暂时还可以接受.但当页面结构复杂起来后,其弱点开始变得无法忍受起来: 书写不连贯.每写一个变量就要断一下,插入一个 + 和 ...

  4. java应用死循环排查方法或查找程序消耗资源的线程方法(面试)

    今天遇到一个面试,怎么在一堆线程中查找一个死循环? 如果遇到线上应用cpu飙升,并出现OutOfMemery怎么办? 首先线上应用的jvm配置要养成良好的习惯,增加一下配置则可以在jvm发生 oom的 ...

  5. (转)javascript中的对象查找

    本文转自:http://otakustay.com/object-lookup-in-javascript/  ---很棒的一篇文章,作者的其他文章还暂时没读,但相信作者是一个谦虚 谨慎的好工程师 近 ...

  6. 论velocity在不同后台语言下的不同

    第一家公司使用asp.net开发的,本人从事前端工作.当时用velocity写模板程序记得也没配置啥,我就记得写了rewrite,html页面里头直接写的velocity. 现在公司用的java开发的 ...

  7. 关于C#不同位数相与或,或赋值时,隐藏位数扩展该留意的问题

    __int64 a; char b; a = b; a |= b; 如上情况,当b的最高位为1时,即b=0x80(或更大)时,b在扩展成64过程中会将最高位向高位扩展变成0xfffffffffffff ...

  8. Java NIO教程 Selector

    这次我们开讲非阻塞I/O中的Selector,它需要配合非阻塞的TCP和UDP来使用.首先我们先简单讲一下TCP和UDP的非阻塞通道. 非阻塞I/O通道 在上代码前我们先讲解一些最基本的知识.TCP和 ...

  9. Diagramming for WinForms 教程一(读取图元数据)

    1,新建“Visual c#” Windows窗体应用程序. 2,从“工具箱”的“Diagramming”选项卡下,托出“DiagramView”控件到Form1上.控件的"Name&quo ...

  10. phpmyadmin

    下载地址:https://www.phpmyadmin.net/ 详情:http://baike.baidu.com/link?url=OIngLv0mpiYTZl_sCEmryWkHgUYqZeHr ...