Last active
March 20, 2018 19:03
-
-
Save findli/dc553614bd0d9dc6922045e5ae22cc12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.InvalidParameterException; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class AlgoDeep { | |
Integer maxHeight; | |
Integer maxHeightIndex; | |
Integer maxNextHeight; | |
Integer maxNextHeightIndex; | |
int lowerElement = Integer.MAX_VALUE; | |
int maxDeep = 0; | |
private void calc(int[] input) { | |
if (input.length < 3) { | |
throw new InvalidParameterException("There must be more than 2 elements in array!"); | |
} | |
for (int i = 0; i < input.length; i++) { | |
setVariables(input, i); | |
if (maxNextHeight != null && maxHeight < maxNextHeight && i > 0) { | |
if (i > 1) | |
setMaxDeep(input, i); | |
startOver(input, i); | |
} | |
if (maxNextHeight != null && maxHeight > maxNextHeight && i > 0) { | |
lowerElement = (input[i] > lowerElement && lowerElement != Integer.MAX_VALUE) ? lowerElement : input[i]; | |
} | |
if (i == input.length - 1) { | |
setMaxDeep(input, i); | |
} | |
} | |
} | |
private void startOver(int[] input, int i) { | |
maxHeight = input[i]; | |
maxHeightIndex = i; | |
maxNextHeight = null; | |
maxNextHeightIndex = null; | |
lowerElement = Integer.MAX_VALUE; | |
} | |
public static void main(String[] args) { | |
int[] input = {2, 4, 3, 3, 5, 1, 2, 1, 2, 3, 1}; | |
AlgoDeep algoDeep = new AlgoDeep(); | |
algoDeep.calc(input); | |
System.out.println(Arrays.toString(input)); | |
System.out.println(algoDeep.maxDeep); | |
assert algoDeep.maxDeep == 2; | |
AlgoDeep algoDeep2 = new AlgoDeep(); | |
int[] input2 = {1, 3, 2, 1, 2, 1, 5, 3, 3, 4, 2}; | |
algoDeep2.calc(input2); | |
System.out.println(Arrays.toString(input2)); | |
System.out.println(algoDeep2.maxDeep); | |
assert algoDeep2.maxDeep == 2; | |
AlgoDeep algoDeep3 = new AlgoDeep(); | |
int[] input1 = {3, 1, 3, 2, 1, 2, 1, 5, 3, 3, 4, 2}; | |
algoDeep3.calc(input1); | |
System.out.println(Arrays.toString(input1)); | |
System.out.println(algoDeep3.maxDeep); | |
assert algoDeep3.maxDeep == 2; | |
AlgoDeep algoDeep4 = new AlgoDeep(); | |
int[] input3 = {3, 5, 2, 4}; | |
algoDeep4.calc(input3); | |
System.out.println(Arrays.toString(input3)); | |
System.out.println(algoDeep4.maxDeep); | |
assert algoDeep4.maxDeep == 2; | |
} | |
private void setVariables(int[] input, int i) { | |
if (maxHeight != null && i != 0) { | |
maxNextHeight = input[i]; | |
maxNextHeightIndex = i; | |
} | |
if (maxHeight == null) { | |
maxHeight = input[i]; | |
maxHeightIndex = i; | |
} | |
} | |
private void setMaxDeep(int[] input, int i) { | |
if (lowerElement == Integer.MAX_VALUE) | |
return; | |
int maxDeepLocal; | |
if (i == input.length - 1 && input[i] < input[i - 1]) { | |
maxDeepLocal = input[i - 1] - lowerElement; | |
} else { | |
if (i == input.length - 1) { | |
maxDeepLocal = input[i] - lowerElement; | |
} else { | |
maxDeepLocal = maxHeight - lowerElement; | |
} | |
} | |
if (maxDeepLocal > maxDeep) maxDeep = maxDeepLocal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment