leetcode 121. Best Time to Buy and Sell Stock ----- java
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.
求最佳买入卖出时的收益。
暴力法,超时。
public class Solution {
public int maxProfit(int[] prices) {int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
for( int i = 0;i<len;i++){
for( int j = i+1;j<len;j++){
if( prices[j]>prices[i])
result = Math.max(result,prices[j]-prices[i]);
} }
return result;
}
}
所以思考一下这道题的原理,其实就设定一个指针就好了
设定刚开始的数字是买入价格:
1、如果高于买入价格,那么就和result相比,取较大的。
2、如果是低于价格,那么更新买入价格。
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
int buy = prices[0];
for( int i = 1;i<len;i++){
if( prices[i] > buy )
{
result = Math.max(result,prices[i]-buy);
}else
buy = prices[i];
} return result; }
}
leetcode 121. Best Time to Buy and Sell Stock ----- java的更多相关文章
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [LeetCode] 121. 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 ...
- Java for LeetCode 121 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 ...
- LeetCode 121. 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 ...
- Python [Leetcode 121]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 ...
- [leetcode]121. 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 ...
- Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
- LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- VisualSVN SERVER的安装和使用
SVN Server安装 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说.下载的网址是:http://subversion.apache.org/packages. ...
- 在同一个页面中加载多个不同的jQuery版本
<!-- 从谷歌服务器加载jQuery最新版本--> <script type="text/javascript" src="http://ajax.g ...
- Win7 Print Spooler服務自动关闭
对于Win7系统而言,该问题通常是安装了错误的打印驱动引起的,Win7系统为了保护其它进程不受干扰,自动关闭了打印服务. 解决方法就是: a> 把不用的打印机删掉. b> 确保你安装了正确 ...
- 支持单色条码图像生成的条形码控件Barcode Professional
Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...
- MySQL数据库远程连接
12.00 MySQL数据库远程连接 参考: http://www.jb51.net/article/24508.htm http://www.linuxdiyf.com/viewarticle.ph ...
- 本周实验的PSP0过程文档
项目计划总结: 日期/任务 听课 编写程序 阅读相关书籍 日总计 周一 110 60 ...
- poj3667 线段树 区间合并
//Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...
- linux添加动态库搜索路径
在有时运行程序出现动态库找不着的问题,而明明装了的.这时候可能是没有将相应的路径添加到系统中去. 具体说:cd /etc/ld.so.conf.d/ 可以发现里面有一堆*.conf的文件 我们要做的就 ...
- 关于oracle存储过程的一些知识点
一.创建一个存储过程,批量清空数据库中所有表的数据. --清空数据库中所有表的数据 create or replace procedure truncateAllTables as v_sql ); ...
- php大力力 [031节] php设计系统后台菜单和样式设计
php大力力 [031节] php设计系统后台菜单和样式设计 耗掉我一整夜的时间,把后台html设计了一个,对于我这样的html白痴,实属不容易啊. 留下一点点网上查找的网页知识: 索马里论坛群发简介 ...