The maximum value of the two element difference in the array (the following element minus the preceding element) O (N) time complexity O (1) Space complexity
Title: Finds two elements in the array, computes the difference between the elements minus the previous one. Find the maximum value for all deviations.
(You may think you are in the stock market, the buy price and the selling price are your profit)
Array A[0...N], for 0<=i<j<=n, find the maximum value of a[j]-a[i]
public class Maxdifference {
public static void Main (string[] arg) {
Int[] A = {12, 20, 23, 1, 2, 5, 7, 10};
Dynamic planning
System.out.println (Maxdifference (a)); Back minus front, max difference
Dynamic planning
Int[] B = {13, 20, 23, 1, 2, 5, 7, 10};
SYSTEM.OUT.PRINTLN ("Dynamic Planning" + MaxDifference1 (b));
Violent search
SYSTEM.OUT.PRINTLN ("Violent Search:" + maxDifference2 (a));
SYSTEM.OUT.PRINTLN ("Violent Search:" + maxDifference2 (b));
Conversion method
System.out.println ("Conversion method:" + MaxDifference4 (a));
}
Bottom-up dynamic planning: O (N)
Minus the number of the previous number.
public static int maxdifference (int[] a) {
int minleft = a[0];//default Minimum value
int maxdiff = a[1]-a[0]; Initial Maximum difference value
for (int i = 2; i < a.length; i++)
{
if (A[i-1] < Minleft)//Get the minimum value of the array before I
{
Minleft = A[i-1];
}
if (A[i]-minleft > MaxDiff)//Get the maximum difference
{
MaxDiff = a[i]-minleft; Lowest value before current value minus current value, maximum difference is obtained
}
}
return MaxDiff;
}
The previous number minus the subsequent number
public static int MaxDifference1 (int[] a) {
int maxleft = a[0];//default Maximum value
int maxdiff = a[0]-a[1]; Initial Maximum difference value
for (int i = 2; i < a.length; i++)
{
if (Maxleft < a[i-1])//Get the maximum value of the array before I
{
Maxleft = A[i-1];
}
if (Maxleft-a[i] > MaxDiff)//Get the maximum difference
{
MaxDiff = Maxleft-a[i]; Subtracts the current value from the maximum value before the current value to get the maximum difference
}
}
return MaxDiff;
}
Divide and Conquer: both the maximum and minimum values are in the left array, the maximum minimum value is in the right array, the maximum minimum value is in the left or right array, respectively
O (NLGN)
public static int MaxDifference3 (int[] a) {
return 0;
}
Conversion method
/*
1, has an array array[n], the array length is n
2. Construct an auxiliary array of length n-1 array2[n-1], and array2[i] = Array[i]-array[i+1]; (0<=i<n-1)
3, if accumulate auxiliary array array2 from I to J (J>i), namely Array[i] + array2[i+1] + ... + array2[j],
(Array[i]-array[i+1]) + (array[i+1]-array[i+2]) + ... + (Array[j]-array[j+1]) = Array[i]-array[j+1]
At this point, it is found that the difference (i.e. Array[i]-array[j+1]) of the largest number pair in the array is actually the sum of the largest contiguous subarray in the auxiliary array array2.
*/
public static int MaxDifference4 (int[] a) {
Int[] B = arrays.copyof (A, a.length); Deep copy of a, length can be customized
int[] temp = new Int[a.length-1];
for (int i = 0; i < temp.length; i + +) {
Temp[i] = a[i+1]-a[i];
}
int sum = 0;
for (int i = 0; i < temp.length; i + +) {
if (sum <= 0) {
sum = Temp[i];
}else {
Sum + = Temp[i];
}
}
return sum;
}
Violent search O (n^2)
public static int MaxDifference2 (int[] a) {
int max = 0;
for (int i = 0; i < a.length-1; i + +) {
for (int j = i + 1; j < A.length; J + +) {
if (A[j]-a[i] > max) {
max = A[j]-a[i];
}
}
}
return Max;
}
Looking for the maximum two number of subscript difference values
Dynamic planning
}
The maximum difference between the following element and the preceding element in the array