fork(21) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static final int[][] DATA = {
  11. { 0, 11, 3, -2, 0, 1, 0, -3, 2, 3 },
  12. {},
  13. {1},
  14. {1,10},
  15. {10,1},
  16. {1, 0, 0,0,0,0,10,20},
  17. {-1, 0, 0,0,0,0,-10,-20},
  18. };
  19.  
  20.  
  21. public static void main(String[] args) {
  22. for (int[] d : DATA) {
  23. System.out.printf("Deepest pit in %s is %d\n", Arrays.toString(d), deepest(d));
  24. }
  25. }
  26.  
  27. private static int deepest(int[] data) {
  28.  
  29. if (data.length < 1) {
  30. return 0;
  31. }
  32.  
  33. int inflection = 0;
  34. int max = 0;
  35. int descent = 0;
  36. boolean ascending = true;
  37. for (int i = 1; i < data.length; i++) {
  38. boolean goingup = data[i] == data[i - 1] ? ascending : data[i] >= data[i - 1];
  39. if (goingup != ascending) {
  40. ascending = goingup;
  41. descent = ascending ? (data[inflection] - data[i - 1]) : 0;
  42. inflection = i - 1;
  43. }
  44.  
  45. max = Math.max(max, Math.min(descent, data[i] - data[inflection]));
  46. }
  47. return max;
  48. }
  49. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
Deepest pit in [0, 11, 3, -2, 0, 1, 0, -3, 2, 3] is 4
Deepest pit in [] is 0
Deepest pit in [1] is 0
Deepest pit in [1, 10] is 0
Deepest pit in [10, 1] is 0
Deepest pit in [1, 0, 0, 0, 0, 0, 10, 20] is 1
Deepest pit in [-1, 0, 0, 0, 0, 0, -10, -20] is 0