-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem4.cs
68 lines (58 loc) · 1.48 KB
/
Problem4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace Problems.Solutions;
public class Problem4
{
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
{
int[] merged = new int[nums1.Length + nums2.Length];
int i1 = 0, i2 = 0, i3 = 0;
while (true)
{
int? num1 = nums1.GetValueOrNull(i1);
int? num2 = nums2.GetValueOrNull(i2);
if (num1 is null && num2 is null)
break;
if (num1 is null)
{
merged[i3] = num2!.Value;
i2++;
}
else if (num2 is null)
{
merged[i3] = num1!.Value;
i1++;
}
else
{
if (num1.Value < num2.Value)
{
merged[i3] = num1.Value;
i1++;
}
else
{
merged[i3] = num2.Value;
i2++;
}
}
i3++;
}
if (merged.Length % 2 == 0)
{
int e1 = (merged.Length - 1) / 2;
int e2 = merged.Length / 2;
return (merged[e1] + merged[e2]) / 2.0;
}
return merged[merged.Length / 2];
}
}
public static class Extensions
{
public static int? GetValueOrNull(this int[] arr, int index)
{
if (index >= 0 && index < arr.Length)
{
return arr[index];
}
return null;
}
}