leetcode 121,122

121Best 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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
             Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

数组是股票价格 股票买卖低价买之后高价卖求最大利润

方法一    第一层循环是卖出时间 第二次循环是买入时间 当买入价格<卖出时计算利润并看是否最大

class Solution {
    public int maxProfit(int[] prices) {
       int max=0;
       
        for(int sall=prices.length-1;sall>0;sall--)
        {
            for(int buy=sall-1;buy>=0;buy--)
            {
                if(prices[buy]

方法2   

class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 0) {
			 return 0 ;
		 }
        int max=0;
       int curmin=prices[0];//当前最小数字
        for(int i=1;icurmin)
            {
                max=Math.max(max,prices[i]-curmin);
            }
            else
            {
                curmin=prices[i];
            }
        }
       
        return max;
    }
}

方法 3 

      Kadane's Algorithm,专门来求最大子序列的和,算法就是考虑,数组中的A[I]前面的的序列段的和为sum,如果sum大于等于零,那么加上A[i],如果sum小于零,就没有必要加了,不如直接保留A[i],这样就相当于从A[i]开始的一个新的数字段。保留一个max保存最大的值,每次都和max比较。

7,1,5,3,6,4   的最大利润     相当于求 0,-6,4,-2,3,-2; 最大子序列的和

class Solution {
    public int maxProfit(int[] prices) {
        int max=0;
        int profit=0;
        for(int i=1;i

122Best 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 (i.e., buy one and sell one share of the stock multiple times).

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

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

在上题的基础上可多次买卖股票

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


你可能感兴趣的:(leetcode)