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. VMware Workstation(虚拟机)v10.0.1 简体中文破解版

    http://www.xp510.com/xiazai/ossoft/desktools/22610.html

  2. Statement及PreparedStatement执行多个sql

        这两个对象的区别: 1.Statement它更适合执行不同sql的批处理,它没有提供预处理功能,性能比较低. 2.PreparedStatement它适合执行相同的批处理,它提供了预处理功能, ...

  3. VerbalExpressions ——另类正则表达式

    对于文本处理来说,正则表达式无疑是一个非常强大的工具.但是编写和阅读正则表达式往往就不是那么一件非常愉快的事情了.本文在这里介绍另一种另类的正则表达式——VerbalExpressions,它采用函数 ...

  4. C++中四种转换类型的区别

    一.四种转换类型比较: 类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可 ...

  5. C++ dll调用

    HINSTANCE PH=LoadLibrary(_T("APlayerCaller.dll")); HWND hwnd = AfxGetMainWnd()->m_hWnd; ...

  6. debian vi

    这次用DigitalOcean VPS发现vi的方向键变成字母,没办法正常使用,搜索了下找到了解决办法. 1 vi /etc/vim/vimrc.tiny 找到set compatible改为set ...

  7. 使用VS,获取SQL SERVER 的链接字符串

    在VS中→工具→链接到数据库→(选择数据源,这里要链接的是SQL server)Microsoft SQL Server→服务器名字(不知道的可以在登陆SQL server的时候,把服务器名字复制过来 ...

  8. Dx unsupported class file version 52.0

    最近用ADT时遇到这个bug,折腾了好几天. 报错信息: Dx unsupported class file version 52.0 Conversion to Dalvik format fail ...

  9. Spark 机器学习

    将Mahout on Spark 中的机器学习算法和MLlib中支持的算法统计如下: 主要针对MLlib进行总结 分类与回归 分类和回归是监督式学习; 监督式学习是指使用有标签的数据(LabeledP ...

  10. [转] Oracle analyze 命令分析

    转自:http://blog.sina.com.cn/s/blog_682841ba0101bncp.html 1.analyze table t1 compute statistics for ta ...