LeetCode OJ: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.
简单的DP问题,可是一开始我没读懂题目的意思。这里的买入当然应该在卖出之前,所以说不是那种取一个max一个min相减就能解决的,代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == || sz == ) return ;
int min, maxGain;
min = prices[];//min的初值要注意
maxGain = ;
for(int i = ; i < sz; ++i){
if(min > prices[i])
min = prices[i];
else if(maxGain < prices[i] - min)
maxGain = prices[i] - min;
}
return maxGain;
}
};
下面是java写的,runtime还行,击败了60%的runtime,方法和上面有一点不同,如下:
public class Solution {
public int maxProfit(int[] prices) {
int maxVal = 0;
int start = 0;
for(int i = 1; i < prices.length; ++i){
if(prices[i] < prices[i-1]){
maxVal = Math.max(prices[i-1]-prices[start], maxVal);
if(prices[i] < prices[start])
start = i;
}
}
if(prices.length != 0)//排除长度是0的情况
maxVal = Math.max(prices[prices.length - 1] - prices[start], maxVal); //防止出现一直到结尾都不断增大的问题
return maxVal;
}
}
LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)的更多相关文章
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 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] 309. 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 IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
随机推荐
- android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:
今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...
- corethink功能模块探索开发(十七)opencmf.php 配置文件
图样: opencmf.php存在于每个模块的根目录,是模块配置文件. 能进行持久化配置参数保存,一开始我以为是写文件或者做缓存,后来在数据库中发现admin_module表,存储了每个模块的配置参数 ...
- ES6简单入门
let let命令用来声明块级作用域. 以前的JavaScript只有全局作用域和函数作用域, 没有块级作用域. // 示例1: if (1) { var a = "hello"; ...
- js 添加css属性
$(".active").css('border','1px solid #ddd')curLi.css('border','2px solid red')curLi.css('b ...
- Codeforces Round #304 (Div.2)
A. Soldier and Bananas 题意:有个士兵要买w个香蕉,香蕉起步价为k元/个,每多买一个则贵k元.问初始拥有n元的士兵需要借多少钱? 思路:简单题 #include<iostr ...
- Windows Live Writer 网易博客配置
一.去官网下载 Windows Live Write 组件 二.配置,选中其它服务,然后会到如下界面 主要是这里经常不知道选择什么,容易忘记. 下拉框选中metaweblog API 类型,把这个地址 ...
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- ASP.NET MVC Select无限级分类选择下拉框
1:读取父级下的所有子类别 *ViewBag.ParentItemList:不能与ParentId相同 private void ParentDropDownList() { List<SAS. ...
- 20145219 《Java程序设计》第10周学习总结
20145219 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 网络概述 1.计算机 ...
- [Ctsc2000]冰原探险
Description 传说中,南极有一片广阔的冰原,在冰原下藏有史前文明的遗址.整个冰原被横竖划分成了很多个大小相等的方格.在这个冰原上有N个大小不等的矩形冰山,这些巨大的冰山有着和南极一样古老的历 ...