Last active
March 12, 2021 22:05
-
-
Save jbasinger/6b91ff76b2a2c76d55f7770bf5b8e509 to your computer and use it in GitHub Desktop.
Leetcode Problem 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A [--|--] | |
* B [--|--] | |
*/ | |
[Test] | |
public void AllOfAIsLessThanB() | |
{ | |
var nums1 = new int[]{1,2,3,4}; | |
var nums2 = new int[]{10,11,12,13,14}; | |
var appended = new int[] {1, 2, 3, 4, 10, 11, 12, 13, 14}; | |
var val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void AllMixedIn() | |
{ | |
var nums1 = new int[] {0, 2, 4, 6, 8}; | |
var nums2 = new int[] {1, 3, 5, 7, 9}; | |
var appended = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
var val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
nums1 = new int[] {2, 4, 6, 8}; | |
nums2 = new int[] {1, 3, 5, 7}; | |
appended = new int[] {1, 2, 3, 4, 5, 6, 7, 8}; | |
val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A [--|--] | |
* B [----|---] | |
*/ | |
[Test] | |
public void AandBMediansAreEqual() | |
{ | |
var nums1 = new int[]{1,1,2,3,4}; | |
var nums2 = new int[]{0,1,2,3,4}; | |
var appended = new int[] {0, 1, 1, 1, 2, 2, 3, 3, 4, 4}; | |
var val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double MedianOfArray(int[] nums) | |
{ | |
if (nums.Length == 0) | |
return 0; | |
if (nums.Length == 1) | |
return nums[0]; | |
if (nums.Length % 2 == 0) | |
{ | |
return ((double) (nums[nums.Length / 2] + nums[(nums.Length / 2) - 1])) / 2; | |
} | |
return nums[nums.Length/2]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A [--|---] | |
* B [---|--] | |
*/ | |
[Test] | |
public void AisInBbutLessThanBMedian() | |
{ | |
var nums1 = new int[]{-1,0,1,2,3,4}; | |
var nums2 = new int[]{0,1,2,3,4}; | |
var appended = new int[] {-1,0,0,1,1,2,2,3,3,4,4}; | |
var val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double FindMedianSortedArrays(int[] nums1, int[] nums2) | |
{ | |
if (nums1.Length == 0 && nums2.Length == 0) | |
return 0; | |
if (nums1.Length == 0) | |
return MedianOfArray(nums2); | |
if (nums2.Length == 0) | |
return MedianOfArray(nums1); | |
var totalLength = nums1.Length + nums2.Length; | |
var arrLength = totalLength / 2+1; | |
var buf = new int[arrLength]; | |
var Ai = 0; | |
var Bi = 0; | |
for (int i = 0; i < arrLength; i++) | |
{ | |
if (Ai == nums1.Length) | |
{ | |
buf[i] = nums2[Bi++]; | |
continue; | |
} | |
if (Bi == nums2.Length) | |
{ | |
buf[i] = nums1[Ai++]; | |
continue; | |
} | |
if (nums1[Ai] < nums2[Bi]) | |
buf[i] = nums1[Ai++]; | |
else | |
buf[i] = nums2[Bi++]; | |
} | |
if (totalLength % 2 == 0) | |
{ | |
return (buf[arrLength - 1] + buf[arrLength - 2]) / 2d; | |
} | |
return buf[arrLength - 1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void WeirdOnesIThoughtWereNeat() | |
{ | |
var nums1 = new int[]{1,1,1,100,100,100}; | |
var nums2 = new int[]{50,50,50}; | |
var appended = new int[] {1,1,1,50,50,50,100,100,100}; | |
var val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
nums1 = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; | |
nums2 = new int[] {1, 2, 3, 4, 5}; | |
appended = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5}; | |
val = MedianOfTwoSortedArrays_Problem.MedianOfArray(appended); | |
_sut.FindMedianSortedArrays(nums1, nums2).ShouldBe(val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment