Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Median in two sorted lists (O(m*n))
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here int[] nums1 = new int[] { 1, 3 }; int[] nums2 = new int[] { 2 }; Solution x = new Solution(); double median = x.FindMedianSortedArrays(nums1, nums2); Console.Write("the median value is {0}", median); } } public class Solution { public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int[] num1 = nums1; int[] num2 = nums2; int lengthOfMergedArray = num1.Count() + num2.Count(); int inaccurateMedianIndexInaccurate = (lengthOfMergedArray / 2) + 1; int currentPointer1 = 0; int currentPointer2 = 0; int currentPointerMerge = 0; if (lengthOfMergedArray == 0) return 0; if (num1.Count() == 0) { if ((num2.Count() % 2 == 1)) { return num2[num2.Count() / 2]; } else { return (double)(num2[(num2.Count() / 2) - 1] + num2[(num2.Count() / 2) - 0]) / 2; } } if (num2.Count() == 0) { if ((num1.Count() % 2 == 1)) { return num1[num1.Count() / 2]; } else { return (double)(num1[(num1.Count() / 2) - 1] + num1[(num1.Count() / 2) - 0]) / 2; } } int previousValue = 0; int currentValue = 0; while (currentPointerMerge < inaccurateMedianIndexInaccurate) { previousValue = currentValue; if (currentPointer2 > num2.Count()) { currentValue = nums1[currentPointer1]; currentPointer1 = currentPointer1 + 1; } else { if ((currentPointer1 > num1.Count())) { currentValue = nums2[currentPointer2]; currentPointer2 = currentPointer2 + 1; } else { if (nums1[currentPointer1] < nums2[currentPointer2]) { currentValue = nums1[currentPointer1]; currentPointer1 = currentPointer1 + 1; } else { currentValue = nums2[currentPointer2]; currentPointer2 = currentPointer2 + 1; } } } currentPointerMerge++; } if ((lengthOfMergedArray % 2 == 1)) { return currentValue; } else { return (double)(currentValue + previousValue) / 2; } } } }
run
|
edit
|
history
|
help
0
Factory Method Design Pattern
Task 2
SDAXXX
anyonymous type pass to method in c#
IsDivisibleV2
C++
aba
merge sort
Exception-Nikhil
Found many section of times intersect (can edit)