假设你有一个数组prices,长度为n,其中prices[i]是股票在第i天的价格,请根据这个价格数组,返回买卖股票能获得的最大收益
1.你可以买入一次股票和卖出一次股票,并非每天都可以买入或卖出一次,总共只能买入和卖出一次,且买入必须在卖出的前面的某一天
2.如果不能获取到任何利润,请返回0
3.假设买入卖出均无手续费
数据范围: 
要求:空间复杂度
,时间复杂度 )
[8,9,2,5,4,7,1]
5
在第3天(股票价格 = 2)的时候买入,在第6天(股票价格 = 7)的时候卖出,最大利润 = 7-2 = 5 ,不能选择在第2天买入,第3天卖出,这样就亏损7了;同时,你也不能在买入前卖出股票。
[2,4,1]
2
[3,2,1]
0
/**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// write code here
if(prices.length<2){return 0}
let len=prices.length;
// let dp=new Array(len);
let max=prices[1]-prices[0];
let min=Math.min(prices[0],prices[1]);
for(let i=2;i<len;i++){
min=Math.min(min,prices[i-1])
max=Math.max(max,prices[i]-min)
}
return Math.max(max,0)
}
module.exports = {
maxProfit : maxProfit
}; /**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// write code here
let res = 0;
let min = prices[0];
for(let i = 0;i < prices.length -1;i++) {
let temp = prices[i]
let next = prices[i+1]
if(temp <= next) {
min = Math.min(min,temp)
res = Math.max(res,next - min)
}
}
return res
}
module.exports = {
maxProfit : maxProfit
}; function maxProfit( prices ) {
// write code here
let dp = [0]
let min = prices[0]
for(let i = 1;i < prices.length;i++) {
min = Math.min(min,prices[i])
dp[i] = Math.max(prices[i] - min,dp[i - 1])
}
return dp[dp.length - 1]
}
module.exports = {
maxProfit : maxProfit
}; function maxProfit( prices ) {
// write code here
let len = prices.length
let dp = new Array(len).fill([0,0])
dp[0] = [-prices[0], 0]
for(let i = 1; i < len; i ++){
dp[i] = [Math.max(dp[i - 1][0], -prices[i]), Math.max(prices[i] + dp[i - 1][0], dp[i - 1][1])]
}
return dp[len - 1][1]
} 动态规划思路整理
/**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit(prices) {
// write code here
// 动态规划三步走
// 1. 确定dp[i]意义: dp[i]取第i天卖出时候的最大收益, 但是计算收益需要有买入和卖出, 卖出很好理解, 也就是当前的prices[i], 买入应该是需要继承之前的dp[i-1]的买入大小
// 2. 确定dp元素关系: dp[i].buy = dp[i-1].buy<prices[i]?dp[i-1].buy:prices[i] dp[i].val = prices[i] - dp[i].buy
// 3. 初始化条件: dp[0] = {val:0, buy:prices[0]}
// 4. 操作
const dp = [{ val: 0, buy: prices[0] }]
let i = 1, money = 0
while (i < prices.length) {
const buy = dp[i - 1].buy < prices[i] ? dp[i - 1].buy : prices[i]
const val = prices[i] - buy
val > money && (money = val)
dp[i++] = { val, buy }
}
return money
}
module.exports = {
maxProfit: maxProfit
};
/**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// write code here
let len = prices.length//数组长度
if(len<=1){return 0}
let max = prices[0];//最大值
let min = prices[0];//最小值
let maxindex = 0;//最大值的下标
let minindex = 0;//最小值的下标
let n = 0;
let m = 0;
prices.map((n,index)=>{
if(n>max){max = n;maxindex = index}
if(n<min){min = n;minindex = index}
})//找出历史最高点和最低点和它们下标
if(minindex<maxindex){return max - min}//如果最小值在左,最大值在右秒出答案
else{//事与愿违
for(let i=minindex;i<len;i++){
if(prices[i]-prices[minindex]>n){
n = prices[i]-prices[minindex]
}
}//从最低点开始向右寻找与最高点的差值
for(let i=maxindex;i>0;i--){
if(prices[maxindex]-prices[i]>n){
m = prices[maxindex]-prices[i]
}
}//从最高点开始向左寻找与最低点的差值
return n>m?n:m
}
}
module.exports = {
maxProfit : maxProfit
}; function maxProfit( prices ) {
// write code here
let result = 0;
for (let i=0; i<prices.length; i++) {
for (let j=i+1; j<prices.length; j++) {
result = prices[j] - prices[i] > result ? prices[j] - prices[i] : result
}
}
return result;
} /**
*
* @param prices int整型一维数组
* @return int整型
*/
function maxProfit( prices ) {
// write code here
let min = prices[0], max = 0;
for(let i = 1; i < prices.length; i++) {
max = Math.max(max, prices[i] - min);//最小值买入的最大获利
min = Math.min(min, prices[i]);//买入的最小值
}
return max;
}
module.exports = {
maxProfit : maxProfit
}; function maxProfit( prices ) {
// write code here
if(prices==null || prices.length==0)
{
return 0
}
var length=prices.length
var dp=[]
//边界条件
dp[0,0]=0
dp[0,1]=-prices[0]
//从第二天开始,因此i=1
for(var i=1;i<length;i++)
{
//递推公式
dp[i,0]=Math.max(dp[i-1,0],dp[i-1,1]+prices[i])
dp[i,1]=Math.max(dp[i-1,1],-prices[i])
}
return dp[length-1,0]
} function maxProfit( prices ) {
// write code here\
if(prices.length <= 1)return 0;
//num1保存最小的数,num2保存最大的差
let num1 = prices[0], num2 = 0;
for(let i = 1; i < prices.length; i++){
if(prices[i] - num1 > num2){
num2 = prices[i] - num1;
}
if(prices[i] < num1){
num1 = prices[i];
}
}
return num2;
}
module.exports = {
maxProfit : maxProfit
}; function maxProfit( prices ) {
// write code here
if(prices.length<=1){
return 0;
}
if(prices.length==2){
return prices[1]-prices[0]>0?prices[1]-prices[0]:0
}
let res = [];
for(let i=0; i<prices.length;i++){
let temp = Math.max(...prices.slice(i, prices.length));
res.push(temp-prices[i])
}
return Math.max(...res);
}
module.exports = {
maxProfit : maxProfit
};