Best Time to Buy and Sell Stock——LeetCode
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.
题目大意:给一个数组,数组的第i个元素是某股票第i天的股价,设计一个算法找出最大的获利。
解题思路:显然买在前,卖在后,这题n^2的做法估计是AC不了的,动规的方式来做吧,F代表获利函数,那么F[i]=price[i]-price[min],其中min<i,如果price[i]<price[min],那么更新min=i。
Talk is cheap>>
public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
int min_pos = 0;
int profit = Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++) {
int tmp_pro = prices[i] - prices[min_pos];
if (prices[i] < prices[min_pos]) {
min_pos = i;
}
profit=Math.max(profit,tmp_pro);
}
return profit;
}
Best Time to Buy and Sell Stock——LeetCode的更多相关文章
- Best Time to Buy and Sell Stock - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...
- Best Time to Buy and Sell Stock leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 121. Best Time to Buy and Sell Stock——Leetcode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode
LeetCode 我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益. 我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主 要是我对sql语言不熟悉).而Java Web开发中有很多orm框架,但 ...
- jquery ajax 提交表单(file && input)
用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...
- Rx RxJava【Operators】操作符
操作符分类 ReactiveX的每种编程语言的实现都实现了一组操作符的集合.不同的实现之间有很多重叠的部分,也有一些操作符只存在特定的实现中.每种实现都倾向于用那种编程语言中他们熟悉的上下文中相似的方 ...
- vijos P1375 大整数(高精不熟的一定要做!)
/* 我尼玛这题不想说啥了 亏了高精写的熟..... 加减乘除max都写了 高精二分 */ #include<iostream> #include<cstdio> #inclu ...
- 【开源java游戏框架libgdx专题】-02-Eclipse Gradle 环境安装
创建eclipse开发环境 Eclipse 4.5 Help -> install newsoftware 填上下载地址(Eclipse 4.5及以上版本): http://dist.sprin ...
- Google与微软为jQuery等类库提供的CDN服务
相关链接: Google: http://code.google.com/apis/ajaxlibs/Microsoft: http://www.asp.net/ajaxlibrary/cdn.a ...
- 7——使用TextView实现跑马灯
首先给TextView添加一个单行限制: android:singleLine="true" - 解决方案一 更改TextView的一个属性: android:ellipsize= ...
- iOS加载启动图的时候隐藏statusbar + 指定启动图显示多少秒
只需需要在info.plist中加入Status bar is initially hidden 设置为YES 补充一下,现在手机越来越快,在6+下面启动图一闪而过,而美工童鞋辛辛苦苦做的图就看不到鸟 ...
- 跳转到QQ聊天界面和QQ群界面
// uin=2977046873为QQ号 NSString *urlString = @"mqq://im/chat?chat_type=wpa&uin=2977046873&am ...
- java_reflect_01
最近学习java开始接触到了框架,突然觉得java反射很重要,因此在这里做了一些总结(参考园中大苞米大神的文章) 首先我们要认识一下Class: 一.Class类有什么用? class类的实例表示ja ...