It's been couple days since I started solving questions on Leetcode. So far I have solved 6 easy questions, 1 medium question and 1 hard question.
When it comes to Array I think it's not that hard for me.
Any way this time was to get the median of two sorted arrays. I obviously thought median was like an average value so I tried coding like that but obviously it wasn't like that.
For those who don't know yet what is the difference between median and average value, please read following explanation.
let's say there is an array of 7 index. The array is already sorted in ascending order.
Array = [1, 2, 3, 4, 200, 300, 99999]
then the median value is just4just because 4 is in the middle of the array.
Whereas the average value should be
Well, that's all about the median value! So here is the question I solved.
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1
Input:nums1 = [1,3],nums2 = [2]
Output:2.00000
Explanation:merged array = [1,2,3]and median is2.
Example 2
Input:nums1 = [1,2],nums2 = [3,4]
Output:2.50000
Explanation:merged array = [1,2,3,4]and median is(2 + 3) / 2=2.5.
And here is my code:
var findMedianSortedArrays = function(nums1, nums2) {
const arr = [];
nums1.forEach(el => arr.push(el));
nums2.forEach(el => arr.push(el));
const ARR = []
const NUMBER = arr.length;
if (arr.length > 1) {
for (let i = 0; i < NUMBER -2; i++) {
let minNum = arr.reduce((acc, origin) => acc > origin ? origin : acc);
ARR.push(minNum);
arr.indexOf(minNum);
arr.splice(arr.indexOf(minNum), 1);
}
} else {
return arr[0]
}
if (arr[0] > arr[1]){
ARR.push(arr[1])
ARR.push(arr[0])
} else {
ARR.push(arr[0])
ARR.push(arr[1])
}
// ascending sorting completed.
console.log(`After: [${ARR}]`)
if ((ARR.length % 2) === 0 ) {
mediumValue = ARR.length / 2
return (ARR[mediumValue-1] + ARR[mediumValue])/2
} else {
mediumValue = ARR.length / 2 - 0.5
return ARR[mediumValue]
}
};
So at first I made an temporary array called arr.
After I pushed all the element inside nums1, nums2 arrays.
Let's say nums1 = [1,5], nums2 = [8, 2]
Hence, arr = [1, 5, 8, 2]
And I made another array ARR, this array will be used as a sorted in ascending order.
Also, I created the constant NUMBER so I can use it for the length of arr. I had to take the value of the length aside because the following Array.prototype.reduce() method would make an impact on the length of arr array.
After that, I made a conditional statement that if the arr's length is bigger than 1, do next, else, just return the arr[0] because there is only one value in arr.
In this case, since the length of arr is bigger than 1, (it's 4) following function will be started.
for (let i = 0; i < NUMBER -2; i++) {
let minNum = arr.reduce((acc, origin) => acc > origin ? origin : acc);
ARR.push(minNum);
arr.indexOf(minNum);
arr.splice(arr.indexOf(minNum), 1);
}
First I had to find either minimum number or maximum number so I can begin to sort the array. I chose to find minumum value because Array.prototype.push() method literally pushes value from right to left. I could use other method like Array.prototype.splice() but I prefered to use former one.
So, with the Array.prototype.reduce() method, if the acc is bigger than origin, origin stays in the array so at last, only the last smallest number would stay.
But wait what? There was a critical issue for this. It just didn't work when there are only 2 index left.
So I had to menually made other conditional statement to finish comparing:
if (arr[0] > arr[1]){
ARR.push(arr[1])
ARR.push(arr[0])
} else {
ARR.push(arr[0])
ARR.push(arr[1])
}
Saying that if the first value is bigger than second value, push second value first and since the first value is bigger it should be pushed at last. And vice versa.
To back to the loop, finally I began to push the minimum numbers ARR.push(minNum). And when the task is finished, I had to delete the minimum number I found from arr so I used Array.prototype.indexOf() to find where it is located, and deleted by using Array.prototype.splice().
Almost done! So the ARR array should be well sorted in ascending order. it should be something like [1, 2, 5, 8].
if ((ARR.length % 2) === 0 ) {
mediumValue = ARR.length / 2
return (ARR[mediumValue-1] + ARR[mediumValue])/2
} else {
mediumValue = ARR.length / 2 - 0.5
return ARR[mediumValue]
}
And at last if ARR's length is even number, I get the middle length and return the value between that two values.
Easy for the odd number, when ARR's length is odd number, I can just find the middle index value and that's it.
This time, I didn't feel that it is difficult. Every time I face new problems, I knew what kind of method to solve those problems.
Let's say I'm doing well, let's keep it up!