יול.23

חציון של מספרים רצים

חציון של מספרים רצים

יש למצוא חציון של זרם של מספרים

  1. public class MedianFinder {
  2.  
  3. double median = 0;
  4. Heap lowers;
  5. Heap greaters;
  6. /** initialize your data structure here. */
  7. public MedianFinder() {
  8. this.lowers = new Heap(MAX_HEAP_FUNC);
  9. this.greaters = new Heap(MIN_HEAP_FUNC);
  10. }
  11. public void AddNum(int num) {
  12. if(lowers.Length == 0 || num < lowers.Peek())
  13. this.lowers.Insert(num);
  14. else
  15. this.greaters.Insert(num);
  16. this.rebalanceHeaps();
  17. this.updateMedian();
  18. }
  19. private void rebalanceHeaps() {
  20. if(lowers.Length - greaters.Length == 2)
  21. this.greaters.Insert(this.lowers.Remove());
  22. else if (greaters.Length - lowers.Length == 2)
  23. this.lowers.Insert(this.greaters.Remove());
  24. }
  25. private void updateMedian() {
  26. if(lowers.Length == greaters.Length)
  27. median = ((double) lowers.Peek() + (double) greaters.Peek()) / 2;
  28. else if(lowers.Length > greaters.Length)
  29. median = lowers.Peek();
  30. else
  31. median = greaters.Peek();
  32. }
  33. public double FindMedian() {
  34. return median;
  35. }
  36. private bool MAX_HEAP_FUNC(int a, int b) {
  37. return a > b;
  38. }
  39. private bool MIN_HEAP_FUNC(int a, int b) {
  40. return a < b;
  41. }
  42.  
  43. }
  44.  
  45.  
  46. public class Heap {
  47. private List<int> heap = new List<int>();
  48. public Func<int, int, bool> ComparisonFunc;
  49. public int Length;
  50. public Heap(Func<int, int, bool> func) {
  51. this.ComparisonFunc = func;
  52. //this.heap = BuildHeap(array);
  53. this.Length = this.heap.Count;
  54. }
  55. public int Peek() {
  56. return heap[0];
  57. }
  58. public int Remove() {
  59. swap(0, heap.Count - 1);
  60. int val = heap[heap.Count - 1];
  61. this.heap.RemoveAt(heap.Count - 1);
  62. this.Length--;
  63. this.SiftDown(0, heap.Count - 1);
  64. return val;
  65. }
  66. public void Insert(int val) {
  67. this.heap.Add(val);
  68. this.Length++;
  69. this.SiftUp(heap.Count - 1);
  70. }
  71. private List<int> BuildHeap(List<int> array) {
  72. int firstParentIdx = (array.Count - 2) / 2; //LAST Parent NODE as start point
  73. for(int i = firstParentIdx; i >= 0; i--) {
  74. this.SiftDown(i, array.Count - 1);
  75. }
  76. return array;
  77. }
  78. private void SiftDown(int currentIdx, int endIdx) {
  79. int childOneIdx = currentIdx * 2 + 1;
  80. while(childOneIdx <= endIdx) {
  81. int childTwoIdx = currentIdx * 2 + 2 <= endIdx ? currentIdx * 2 + 2 : -1;
  82. int idxToSwap;
  83. if(childTwoIdx != -1 && ComparisonFunc(heap[childTwoIdx], heap[childOneIdx]))
  84. idxToSwap = childTwoIdx;
  85. else
  86. idxToSwap = childOneIdx;
  87. if(ComparisonFunc(heap[idxToSwap], heap[currentIdx])) {
  88. swap(currentIdx, idxToSwap);
  89. currentIdx = idxToSwap;
  90. childOneIdx = currentIdx * 2 + 1;
  91. }
  92. else return; //nothing to siftDown anymore
  93. }
  94. }
  95. private void SiftUp(int currentIdx) {
  96. int parentIdx = (currentIdx - 1) / 2;
  97. while(currentIdx > 0) {
  98. if(ComparisonFunc(heap[currentIdx], heap[parentIdx])) {
  99. swap(currentIdx, parentIdx);
  100. currentIdx = parentIdx;
  101. parentIdx = (currentIdx - 1) / 2;
  102. }
  103. }
  104. }
  105. private void swap(int i, int j) {
  106. int temp = heap[i];
  107. heap[i] = heap[j];
  108. heap[j] = temp;
  109. }
  110. }
  1. public class MedianFinder {
  2. private readonly List<int> store = new List<int>();
  3.  
  4. public void AddNum(int num)
  5. {
  6. int index = store.BinarySearch(num);
  7. index = index >= 0 ? index : ~index;
  8. store.Insert(index, num);
  9. }
  10.  
  11. public double FindMedian()
  12. {
  13. int count = store.Count;
  14. return count % 2 != 0 ? store[count / 2] : ((double)(store[count / 2 - 1] + store[count / 2])) / 2;
  15. }
  16. }


תגיות:
שתף את הסיפור הזה:

תגובות(0)

השאירו תגובה

קפטצ'ה לא מתאימה

תגובה