Guest User

Untitled

a guest
Nov 21st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. class Solution {
  2. public double[] medianSlidingWindow(int[] nums, int k) {
  3. int[] arr = nums;
  4. return calculateMedian(arr,k);
  5. }
  6.  
  7. private double[] calculateMedian(int[] arr, int k) {
  8.  
  9. double[] output = new double[arr.length - k + 1];
  10. int startIndex = 0;
  11. int endIndex = k-1;
  12. int index =0;
  13. while(endIndex<arr.length){
  14. output[index]=calculateMedianWindow(arr,startIndex,endIndex,k);
  15. startIndex++;
  16. endIndex++;
  17. index++;
  18. }
  19. return output;
  20. }
  21.  
  22.  
  23.  
  24. private double calculateMedianWindow(int[] arr,int startIndex, int endIndex,int k) {
  25. int[] numbers = new int[k];
  26. int i = startIndex;
  27. int index = 0;
  28. while(i<=endIndex){
  29. numbers[index]= arr[i];
  30. i++;
  31. index++;
  32. }
  33. double median =0;
  34. quicksort(0,k-1,numbers);
  35. if(k%2==0){
  36. double val = add(numbers[(numbers.length - 1) / 2] , numbers[numbers.length / 2])/2.0;
  37. if(val>=Integer.MAX_VALUE){
  38. median = Integer.MAX_VALUE;
  39. }else if( val <= Integer.MIN_VALUE){
  40. median = Integer.MIN_VALUE;
  41. }else {
  42. median = val;
  43. }
  44. }else{
  45. double val = ((double)numbers[numbers.length / 2]);
  46. if(val>=Integer.MAX_VALUE){
  47. median = Integer.MAX_VALUE;
  48. }else if( val <= Integer.MIN_VALUE){
  49. median = Integer.MIN_VALUE;
  50. }else {
  51. median = val;
  52. }
  53. }
  54. return median;
  55. }
  56.  
  57. private void quicksort(int low, int high,int[] numbers) {
  58. int i = low, j = high;
  59. // Get the pivot element from the middle of the list
  60. int pivot = numbers[low + (high-low)/2];
  61.  
  62. // Divide into two lists
  63. while (i <= j) {
  64. // If the current value from the left list is smaller than the pivot
  65. // element then get the next element from the left list
  66. while (numbers[i] < pivot) {
  67. i++;
  68. }
  69. // If the current value from the right list is larger than the pivot
  70. // element then get the next element from the right list
  71. while (numbers[j] > pivot) {
  72. j--;
  73. }
  74.  
  75. // If we have found a value in the left list which is larger than
  76. // the pivot element and if we have found a value in the right list
  77. // which is smaller than the pivot element then we exchange the
  78. // values.
  79. // As we are done we can increase i and j
  80. if (i <= j) {
  81. exchange(i, j,numbers);
  82. i++;
  83. j--;
  84. }
  85. }
  86. // Recursion
  87. if (low < j)
  88. // sort left part of array
  89. quicksort(low, j,numbers);
  90. if (i < high)
  91. // sort right part of array
  92. quicksort(i, high,numbers);
  93. }
  94. private void exchange(int i, int j,int[] numbers) {
  95. int temp = numbers[i];
  96. numbers[i] = numbers[j];
  97. numbers[j] = temp;
  98. }
  99.  
  100. private double add(int a, int b) {
  101. return ((double) a) + (double)b;
  102. }
  103. }
Add Comment
Please, Sign In to add comment