Skip to content

Commit 688fddd

Browse files
committed
Edits.
1 parent c829723 commit 688fddd

File tree

1 file changed

+3
-3
lines changed
  • src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch10_TwoHeaps/P1_FindTheMedianOfANumberStream

1 file changed

+3
-3
lines changed

src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch10_TwoHeaps/P1_FindTheMedianOfANumberStream/readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Example 1:
2323

2424
## Discussion
2525

26-
As we know, the median is the middle value in an ordered integer list. So a brute force solution could be to maintain a sorted list of all numbers inserted in the class so that we can efficiently return the median whenever required. Inserting a number in a sorted list will take O(N)O(N) time if there are ‘N’ numbers in the list. This insertion will be similar to the [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort). Can we do better than this? Can we utilize the fact that we don’t need the fully sorted list - we are only interested in finding the middle element?
26+
As we know, the median is the middle value in an ordered integer list. So a brute force solution could be to maintain a sorted list of all numbers inserted in the class so that we can efficiently return the median whenever required. Inserting a number in a sorted list will take O(N) time if there are ‘N’ numbers in the list. This insertion will be similar to the [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort). Can we do better than this? Can we utilize the fact that we don’t need the fully sorted list - we are only interested in finding the middle element?
2727

2828
Assume ‘x’ is the median of a list. This means that half of the numbers in the list will be smaller than (or equal to) ‘x’ and half will be greater than (or equal to) ‘x’. This leads us to an approach where we can divide the list into two halves: one half to store all the smaller numbers (let’s call it `smallNumList`) and one half to store the larger numbers (let’s call it `largNumList`). The median of all the numbers will either be the largest number in the `smallNumList` or the smallest number in the `largNumList`. If the total number of elements is even, the median will be the average of these two numbers.
2929

@@ -72,7 +72,7 @@ Let’s take the Example-1 mentioned above to go through each step of our algori
7272
}
7373
```
7474
75-
3. `findMedian()`: As we have an even number of elements, the median will be the average of the top element of both the heaps -> (1+3)/2 = 2.0(1+3)/2=2.0
75+
3. `findMedian()`: As we have an even number of elements, the median will be the average of the top element of both the heaps -> (1+3)/2 = 2.0
7676
4. `insertNum(5)`: As ‘5’ is greater than the top element of the Max Heap, we can insert it into the Min Heap. After the insertion, the total count of elements will be odd. As we had decided to have more numbers in the Max Heap than the Min Heap, we can take the top (smallest) number from the Min Heap and insert it into the Max Heap.
7777
7878
```plantuml
@@ -98,7 +98,7 @@ Let’s take the Example-1 mentioned above to go through each step of our algori
9898
}
9999
```
100100
101-
7. `findMedian()`: As we have an even number of elements, the median will be the average of the top element of both the heaps -> (3+4)/2 = 3.5(3+4)/2=3.5
101+
7. `findMedian()`: As we have an even number of elements, the median will be the average of the top element of both the heaps -> (3+4)/2 = 3.5
102102
103103
### Time Complexity
104104

0 commit comments

Comments
 (0)