Problem:
You are given array of numbers where index significance is date and the value at index of array is the value of share. Find the maximum profit any person can make by buying a share on x day and selling on y day where y > x, assuming you have complete data from all of the days specified in array before in hand.
Solution:
Approach : Simple One Pass
Algorithm
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]
6 + 0 + 1+3 + 1 + 0 + 1 = 12
Time complexity : O(n)
Space complexity: O(1)
You are given array of numbers where index significance is date and the value at index of array is the value of share. Find the maximum profit any person can make by buying a share on x day and selling on y day where y > x, assuming you have complete data from all of the days specified in array before in hand.
Solution:
Approach : Simple One Pass
Algorithm
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]
6 + 0 + 1+3 + 1 + 0 + 1 = 12
class Project1 {
public static void main(String args[])
{
int a[] = new int[8];
a = [1, 7, 2, 3, 6, 7, 6, 7];
int maxProfitGained = maxProfit(a);
System.out.println("The Gain anyone can make in this peaks and valleys are:"+maxProfitGained);
}
public int maxProfit(int[] prices) {
int maxprofit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
}
}
Complexity Analysispublic static void main(String args[])
{
int a[] = new int[8];
a = [1, 7, 2, 3, 6, 7, 6, 7];
int maxProfitGained = maxProfit(a);
System.out.println("The Gain anyone can make in this peaks and valleys are:"+maxProfitGained);
}
public int maxProfit(int[] prices) {
int maxprofit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
}
}
Time complexity : O(n)
Space complexity: O(1)
No comments:
Post a Comment