Leetcode: 121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell Stock II

121

Question:

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.

Solution:

class Solution {
    public int maxProfit(int[] prices) {
    	if (prices.length == 0 || prices.length == 1) {
    		return 0;
    	}
    	int maxPro = 0;
    	int min = prices[0];
    	for (int price : prices) {
    		if(price < min) {
    			min = price;
    		} else if(price - min > maxPro) {
    			maxPro = price - min;
    		}
    	}
    	return maxPro;
    }
}

122

Question:

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.

Note 1:

Say the given array is:
[7, 1, 5, 3, 6, 4].
If we plot the numbers of the given array on a graph, we get:在这里插入图片描述The key point is we need to consider every peak immediately following a valley(understand the meaning of peak and valley) to maximize the profit. In case we skip one of the peaks (trying to obtain more profit), we will end up losing the profit over one of the transactions leading to an overall lesser profit.

For example, in the above case, if we skip peaki and valleyj, trying to obtain more profit by considering points with more difference in heights, the net profit obtained will always be lesser than the one obtained by including them, since C will always be lesser than A+B.

Solution 1:

class Solution {
    public int maxProfit(int[] prices) {
    	int maxPro = 0;
    	int peak = prices[0];
    	int valley = prices[0];
    	int i = 1;
    	while (i < prices.length - 1) {
    	// starting to look for valley.
    	// We need to look for valley first, then peak
    		while(i < prices.length - 1 && prices[i + 1] <= prices[i]) {
    			i++;
    		}
    		valley = prices[i];
    		while(i < prices.length - 1 && prices[i + 1] >= prices[i]) {
    			i++;
    		}
    		peak = prices[i];
    		maxPro += peak - valley;
    	}
    	return maxPro;
    }
}

Note 2:

In this case, instead of looking for every peak following a valley, we can simply go on crawling over the slope and keep on adding the profit obtained from every consecutive transaction. In the end,we will be using the peaks and valleys effectively, but we need not track the costs corresponding to the peaks and valleys along with the maximum profit, but we can directly keep on adding the difference between the consecutive numbers of the array if the second number is larger than the first one, and at the total sum we obtain will be the maximum profit. This approach will simplify the solution. This can be made clearer by taking this example:
[1, 7, 2, 3, 6, 7, 6, 7]
The graph corresponding to this array is:
在这里插入图片描述
From the above graph, we can observe that the sum A+B+C is equal to the difference D corresponding to the difference between the heights of the consecutive peak and valley.

Solution 2:

class Solution {
    public int maxProfit(int[] prices) {
    	int maxPro = 0;
    	for (int i = 1; i < prices.length; i++) {
    		if (prices[i] > prices[i - 1]) {
    			maxPro += prices[i] - prices[i - 1];
    		}
    	}
    	return maxPro;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值