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 ...
随机推荐
- 为Eclipse指定JVM
运行eclipse时,报如下错误时,可以通过修改配置文件eclipse.ini来解决. Version 1.4.1_01 of the JVM is not suitable for this pro ...
- html-3,table 表格标签 tr th td caption thead tbody tfoot 的简单使用
<!-- table border='1' style="border-collapse:collapse;" border 表格的像素宽度 border-collapse: ...
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- linux例行性工作调度学习(一)
Linux系统中有一种例行性工作(crontab)可以调度,是通过crontab和at来实现的. 这两种工作调度: 一种是例行性的,就是每隔一定的周期要来办的事项. 一种是突发性的,就是这次做完以后就 ...
- Java伪代码示例
学习并转载自https://www.cnblogs.com/z245894546/p/7535261.html import.java.大道至简.*; import.java.愚公移山.*; publ ...
- 浏览器 Event对象 及 属性 的兼容处理
摘自: http://blog.csdn.net/jiachunfeng/article/details/6448186 event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以, ...
- react下将输入的汉字转化为拼音
1.首先需要一个简单的拼音和汉字对应的字典文件: /** * 收录常用汉字6763个,不支持声调,支持多音字,并按照汉字使用频率由低到高排序 */ var pinyin_dict_notone = { ...
- Spark机器学习3·推荐引擎(spark-shell)
Spark机器学习 准备环境 jblashttps://gcc.gnu.org/wiki/GFortranBinaries#MacOS org.jblas:jblas:1.2.4-SNAPSHOT g ...
- 20145109《Java程序设计》第二周学习总结
20145109 <Java程序设计>第二周学习总结 教材学习内容总结 Variable : naming rule : Camel case no default value e.g : ...
- Spring_使用外部属性文件
beans-properties.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...