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 ...
随机推荐
- cocos2d-x 背景音乐播放
Code // on "init" you need to initialize your instance bool HelloWorld::init() { bool ...
- Android(java)学习笔记218:开发一个多界面的应用程序之人品计算器的简单实现
1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义") 记得在清单文件中声明 (3)intent.setData(前 ...
- codevs1955光纤通信(并查集)
/* 第一眼以为就是个区间覆盖 然后敲完提交60分0.0 然而觉得自己的做法很对 以为数据错了 后来发现XXX他的牛棚是一圈(牛过得挺好的啊 还能赏湖...) 然后枚举断开的点 可惜n=750 p=1 ...
- HDU5317
题意:定义一个数K,最小质因数形式为K = a*b*c形式(如12 = 2*2*3),相同只取一个(所以12只能取2,3两个,既F[12]=2)给L,R区间,找出区间内最大的F[x](L<=x& ...
- 系统spt_values表--生成时间方便left join
时间处理我给你提供一个思路 系统有个spt_values表,可以构造一整个月的日期,然后左连接你统计好的数据,用CTE表构造多次查询 spt_values的超级经典的应用 http://www. ...
- [转帖]AVS音视频编解码技术了解
AVS高清立体视频编码器 电视技术在经历了从黑白到彩色.从模拟到数字的技术变革之后正在酝酿另一场技术革命,从单纯观看二维场景的平面电视跨越到展现三维场景的立体电视3DTV.3DTV系统的核心问题之一是 ...
- socket.io实现
后台代码 index_server.js var app = require('http').createServer(handler)//创建服务器app , io = require('socke ...
- 泛型,迭代器,LinkedList<E>
1 <e>里面只能填类,不能用基本数据类型,不过integer 这样的的也行 2在模板类(泛型类中)class demo<e>由于不知道e是那个,所有通常都是重写大家都有的to ...
- jQuery图片滑动
一个非常简单实用的jQuery插件 可以用在页面的顶部广告展示 http://slidesjs.com/ 一个需要注意的问题, 就是在手机等客户端(IOS8以上), 使用此插件时, 经常会触发插件的r ...
- jquery mobile展开项collapsible
代码演示 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...