Created
December 18, 2017 01:32
-
-
Save jianminchen/4367873050babb30cc5a7b9f6af1507e to your computer and use it in GitHub Desktop.
The peer complained that the API I posted on code review does not work. Need to work on the issue.
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
It was my pleasure meeting you. Wow you are smart if you can solve this problem! I sure couldn’t. | |
So I tried your solution in leetcode and I’m not quite getting 100% pass. Am I calling it incorrectly? | |
This Is my calling code: | |
4. Median of Two Sorted Arrays | |
https://leetcode.com/problems/median-of-two-sorted-arrays/description/ | |
public double FindMedianSortedArrays(int[] A, int[] B) | |
{ | |
int n = A.Length; | |
int m = B.Length; | |
bool isEven = (m + n) % 2 == 0; | |
int l = 0; | |
int r = m + n - 1; | |
int mi1 = l + (r - l) / 2; | |
int mi2 = mi1 + 1; | |
if (n == 0) | |
return (!isEven) ? B[mi1] : (B[mi1] + B[mi2]) / 2.0; | |
if (m == 0) | |
return (!isEven) ? A[mi1] : (A[mi1] + A[mi2]) / 2.0; | |
if (isEven) | |
{ | |
double mVal1 = FindKthLargestElement(A, B, mi1); | |
double mVal2 = FindKthLargestElement(A, B, mi2); | |
return (mVal1 + mVal2) / 2.0; | |
} | |
else | |
{ | |
return FindKthLargestElement(A, B, mi1); | |
} | |
} | |
Input:[1,3] | |
[2] | |
Output:1.00000 | |
Expected:2.00000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment