|
4 | 4 | * @return {number}
|
5 | 5 | */
|
6 | 6 | const findMedianSortedArrays = (nums1, nums2) => {
|
7 |
| -// TODO: |
| 7 | + // |
| 8 | + let list = nums1.concat(nums2); |
| 9 | + console.log(list) |
| 10 | + |
| 11 | + // // console.log(nums1.concat(nums2).splice()) |
| 12 | + // for (let i = 0; i < list.length; i++) { |
| 13 | + // // Find the name of the sort. - bubble sort |
| 14 | + // |
| 15 | + // |
| 16 | + // } |
| 17 | + let sortedList = reconstructList(nums1.concat(nums2), []); |
| 18 | + console.log("sortedList") |
| 19 | + console.log(sortedList) |
| 20 | + console.log(sortedList.length) |
| 21 | + if (Number.isInteger(sortedList.length / 2)) { |
| 22 | + console.log(`1 -> ${(sortedList[sortedList.length / 2] + sortedList[(sortedList.length / 2) - 1]) / 2}`) |
| 23 | + return (sortedList[sortedList.length / 2] + sortedList[(sortedList.length / 2) - 1]) / 2; |
| 24 | + } else { |
| 25 | + console.log(`2 -> ${(sortedList.length / 2) + .5}`) |
| 26 | + console.log(`2 -> ${sortedList[(sortedList.length / 2) - .5]}`) |
| 27 | + return sortedList[(sortedList.length / 2) - .5]; |
| 28 | + } |
| 29 | + |
8 | 30 | };
|
| 31 | + |
| 32 | +const reconstructList = (numsList, sortedList) => { |
| 33 | + if (0 === numsList.length) return sortedList; |
| 34 | + let first = numsList[0], |
| 35 | + biggerThanList = []; |
| 36 | + numsList.shift(); |
| 37 | + |
| 38 | + console.log(`first: ${first}, numsList: ${numsList}, sortedList: ${sortedList}`) |
| 39 | + |
| 40 | + for (let i = 0; i < numsList.length; i++) { |
| 41 | + if (numsList[i] > first) { |
| 42 | + biggerThanList = [...biggerThanList, numsList[i]]; |
| 43 | + } else { |
| 44 | + sortedList = [...sortedList, numsList[i]]; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + console.log("biggerThanList") |
| 49 | + console.log(biggerThanList) |
| 50 | + console.log("sortedList") |
| 51 | + console.log(sortedList) |
| 52 | + |
| 53 | + |
| 54 | + // return reconstructList([...numsList, first], sortedList); |
| 55 | + return reconstructList(biggerThanList, [...sortedList, first]); |
| 56 | +} |
| 57 | + |
| 58 | +// findMedianSortedArrays([1, 2, 3, 4], [3, 6, 7]) |
| 59 | +// findMedianSortedArrays([1, 2, 3, 4], [3, 4, 7, 9]) |
| 60 | +// findMedianSortedArrays([9, 10, 13, 75], [1, 2, 3, 4, 7, 61]) |
| 61 | +// findMedianSortedArrays([2, 3], [1]) |
| 62 | +// findMedianSortedArrays([2, 3], [1]) |
0 commit comments