|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import { |
| 4 | + findMedianSortedArrays, |
| 5 | + findMedianSortedArraysMerge, |
| 6 | + findMedianSortedArraysTwoPointers, |
| 7 | +} from './solution'; |
| 8 | + |
| 9 | +describe('LeetCode 4 - Median of Two Sorted Arrays', () => { |
| 10 | + describe('findMedianSortedArrays (Binary Search - Optimal)', () => { |
| 11 | + it('should return 2.0 for nums1=[1,3], nums2=[2]', () => { |
| 12 | + assert.strictEqual(findMedianSortedArrays([1, 3], [2]), 2.0); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return 2.5 for nums1=[1,2], nums2=[3,4]', () => { |
| 16 | + assert.strictEqual(findMedianSortedArrays([1, 2], [3, 4]), 2.5); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should handle empty first array', () => { |
| 20 | + assert.strictEqual(findMedianSortedArrays([], [1]), 1.0); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should handle empty second array', () => { |
| 24 | + assert.strictEqual(findMedianSortedArrays([2], []), 2.0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle single element arrays', () => { |
| 28 | + assert.strictEqual(findMedianSortedArrays([1], [2]), 1.5); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should handle arrays of different lengths', () => { |
| 32 | + assert.strictEqual(findMedianSortedArrays([1, 2, 3], [4, 5, 6, 7, 8]), 4.5); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle duplicate values', () => { |
| 36 | + assert.strictEqual(findMedianSortedArrays([1, 2, 2], [2, 2, 3]), 2.0); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle negative numbers', () => { |
| 40 | + assert.strictEqual(findMedianSortedArrays([-3, -1], [-2]), -2.0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle mixed positive and negative numbers', () => { |
| 44 | + assert.strictEqual(findMedianSortedArrays([-2, -1], [3, 4]), 1.0); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle large numbers', () => { |
| 48 | + assert.strictEqual( |
| 49 | + findMedianSortedArrays([100000], [100001]), |
| 50 | + 100000.5 |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle non-overlapping arrays (first smaller)', () => { |
| 55 | + assert.strictEqual(findMedianSortedArrays([1, 2], [5, 6]), 3.5); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle non-overlapping arrays (second smaller)', () => { |
| 59 | + assert.strictEqual(findMedianSortedArrays([5, 6], [1, 2]), 3.5); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle interleaved arrays', () => { |
| 63 | + assert.strictEqual(findMedianSortedArrays([1, 3, 5], [2, 4, 6]), 3.5); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle all same elements', () => { |
| 67 | + assert.strictEqual(findMedianSortedArrays([2, 2, 2], [2, 2, 2]), 2.0); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle odd total length', () => { |
| 71 | + assert.strictEqual(findMedianSortedArrays([1, 3], [2, 4, 5]), 3.0); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('findMedianSortedArraysMerge (Merge and Find)', () => { |
| 76 | + it('should return 2.0 for nums1=[1,3], nums2=[2]', () => { |
| 77 | + assert.strictEqual(findMedianSortedArraysMerge([1, 3], [2]), 2.0); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return 2.5 for nums1=[1,2], nums2=[3,4]', () => { |
| 81 | + assert.strictEqual(findMedianSortedArraysMerge([1, 2], [3, 4]), 2.5); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle empty first array', () => { |
| 85 | + assert.strictEqual(findMedianSortedArraysMerge([], [1]), 1.0); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle empty second array', () => { |
| 89 | + assert.strictEqual(findMedianSortedArraysMerge([2], []), 2.0); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle single element arrays', () => { |
| 93 | + assert.strictEqual(findMedianSortedArraysMerge([1], [2]), 1.5); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle arrays of different lengths', () => { |
| 97 | + assert.strictEqual( |
| 98 | + findMedianSortedArraysMerge([1, 2, 3], [4, 5, 6, 7, 8]), |
| 99 | + 4.5 |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle duplicate values', () => { |
| 104 | + assert.strictEqual(findMedianSortedArraysMerge([1, 2, 2], [2, 2, 3]), 2.0); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle negative numbers', () => { |
| 108 | + assert.strictEqual(findMedianSortedArraysMerge([-3, -1], [-2]), -2.0); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should handle mixed positive and negative numbers', () => { |
| 112 | + assert.strictEqual(findMedianSortedArraysMerge([-2, -1], [3, 4]), 1.0); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle interleaved arrays', () => { |
| 116 | + assert.strictEqual(findMedianSortedArraysMerge([1, 3, 5], [2, 4, 6]), 3.5); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle all same elements', () => { |
| 120 | + assert.strictEqual(findMedianSortedArraysMerge([2, 2, 2], [2, 2, 2]), 2.0); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle odd total length', () => { |
| 124 | + assert.strictEqual(findMedianSortedArraysMerge([1, 3], [2, 4, 5]), 3.0); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('findMedianSortedArraysTwoPointers (Two Pointers)', () => { |
| 129 | + it('should return 2.0 for nums1=[1,3], nums2=[2]', () => { |
| 130 | + assert.strictEqual(findMedianSortedArraysTwoPointers([1, 3], [2]), 2.0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should return 2.5 for nums1=[1,2], nums2=[3,4]', () => { |
| 134 | + assert.strictEqual(findMedianSortedArraysTwoPointers([1, 2], [3, 4]), 2.5); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should handle empty first array', () => { |
| 138 | + assert.strictEqual(findMedianSortedArraysTwoPointers([], [1]), 1.0); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle empty second array', () => { |
| 142 | + assert.strictEqual(findMedianSortedArraysTwoPointers([2], []), 2.0); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should handle single element arrays', () => { |
| 146 | + assert.strictEqual(findMedianSortedArraysTwoPointers([1], [2]), 1.5); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should handle arrays of different lengths', () => { |
| 150 | + assert.strictEqual( |
| 151 | + findMedianSortedArraysTwoPointers([1, 2, 3], [4, 5, 6, 7, 8]), |
| 152 | + 4.5 |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should handle duplicate values', () => { |
| 157 | + assert.strictEqual( |
| 158 | + findMedianSortedArraysTwoPointers([1, 2, 2], [2, 2, 3]), |
| 159 | + 2.0 |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle negative numbers', () => { |
| 164 | + assert.strictEqual( |
| 165 | + findMedianSortedArraysTwoPointers([-3, -1], [-2]), |
| 166 | + -2.0 |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should handle mixed positive and negative numbers', () => { |
| 171 | + assert.strictEqual( |
| 172 | + findMedianSortedArraysTwoPointers([-2, -1], [3, 4]), |
| 173 | + 1.0 |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should handle interleaved arrays', () => { |
| 178 | + assert.strictEqual( |
| 179 | + findMedianSortedArraysTwoPointers([1, 3, 5], [2, 4, 6]), |
| 180 | + 3.5 |
| 181 | + ); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should handle all same elements', () => { |
| 185 | + assert.strictEqual( |
| 186 | + findMedianSortedArraysTwoPointers([2, 2, 2], [2, 2, 2]), |
| 187 | + 2.0 |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should handle odd total length', () => { |
| 192 | + assert.strictEqual( |
| 193 | + findMedianSortedArraysTwoPointers([1, 3], [2, 4, 5]), |
| 194 | + 3.0 |
| 195 | + ); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments