zoukankan      html  css  js  c++  java
  • LeetCode Best Time to Buy and Sell Stock I II III

    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.

    题意:做一次买入卖出的最大收益。

    思路:一次线性扫描解决。

    public class Solution {
        public int maxProfit(int[] prices) {
        	if (prices.length == 0) return 0;
        	
        	int low = prices[0];
        	int ans = 0;
        	for (int i = 1; i < prices.length; i++) {
        		if (prices[i] < low) low = prices[i];
        		else if (ans < prices[i] - low) ans = prices[i] - low;
        	}
        	
        	return ans;
        }
    }

    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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    题意:能够不断买入卖出的最大收益。

    思路:按理说找出每段上升序列的头尾差。可是能够发现事实上头尾差就是每相邻的差(保证后一位较大)。

    public class Solution {
        public int maxProfit(int[] prices) {
            if (prices.length == 0) return 0;
            
        	int ans = 0;
        	for (int i = 0; i < prices.length - 1; i++) {
        		if (prices[i+1] > prices[i]) 
        			ans += prices[i+1] - prices[i];
        	}
        	
        	return ans;
        }
    }

    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 algorithm to find the maximum profit. You may complete at most two transactions.

    Note:
    You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    题意:仅仅能买入卖出两次的最大收益。

    思路:注意卖出和买入能够同一时刻,两次就意味着把序列切成了两部分,那么一次从前遍历运行第一题的操作,再从后来一次,求和的最大值。

    public class Solution {
        public int maxProfit(int[] prices) {
            if (prices.length == 0) return 0;
        	
        	final int len = prices.length;
        	int low = prices[0], Max = 0;
        	int[] profit = new int[len];
        	profit[0] = 0;
        	for (int i = 1; i < len; i++) {
        		low = Math.min(low, prices[i]);
        		if (Max < prices[i] - low) Max = prices[i] - low;
        		profit[i] = Max;
        	}
        	
        	int tmpMax = prices[len-1];
        	int ans = profit[len-1];
        	Max = 0;
        	for (int i = len-2; i >= 0; i--) {
        		tmpMax = Math.max(tmpMax, prices[i]);
        		if (Max < tmpMax - prices[i])
        			Max = tmpMax - prices[i];
        		if (ans < profit[i] + Max)
        			ans = profit[i] + Max;
        	}
        	
        	return ans;
        }
    }


  • 相关阅读:
    BZOJ 1433 && Luogu P2055 [ZJOI2009]假期的宿舍 匈牙利算法
    BZOJ 1123 && Luogu P3469 [POI2008]BLO-Blockade 割点+乘法原理
    POJ3694 Network 边双缩点+LCA+并查集
    luogu P5142 区间方差 十分优美的线段树
    luogu P2709 小B的询问 最简单的莫队
    luogu P2731 骑马修栅栏 Riding the Fences
    TYVJ P2032 「Poetize9」升降梯上 spfa最短路
    51nod 1515 明辨是非 并查集+set维护相等与不等关系
    BZOJ 1260: [CQOI2007]涂色paint 区间DP
    luogu P4145 上帝造题的七分钟2 / 花神游历各国 维护区间和&&区间开根号
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5227617.html
Copyright © 2011-2022 走看看