You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/array/004-median-of-two-sorted-arrays.md
+22-28Lines changed: 22 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,20 +32,11 @@ The median is (2 + 3)/2 = 2.5
32
32
1. Knowing the length of both array, we can use a counter to count the element until it reach n/2 for odd number or n/2 + 1, and taking average for the second case
33
33
2. Time complexity will be O\(m+ n\)
34
34
3. Space complexity will be O\(1\)
35
-
2. Optimal \(Binary Search\) 1. Because the question warrants O\(log\(m+n\)\), binary search is probably the desired approach 2. Looking back at the definition of median of an array, it's located in the middle 3. If we can cut an array into two puts, and call the element immediately left to the cut L, and right to the cut the R, we can find the median easily by average them 1. \[2 3 / 5 7\] -> median = \(3+5\)/2 2. \[2 3 \(4/4\) 5 7\] -> median = \(4 + 4\) / 2 = 4
36
-
1. The index can be listed as follow
37
-
1.```text
38
-
N Index of L / R
39
-
1 0 / 0
40
-
2 0 / 1
41
-
3 1 / 1
42
-
4 1 / 2
43
-
5 2 / 2
44
-
6 2 / 3
45
-
7 3 / 3
46
-
8 3 / 4
47
-
```
35
+
2. Optimal \(Binary Search\) 1. Because the question warrants O\(log\(m+n\)\), binary search is probably the desired approach 2. Looking back at the definition of median of an array, it's located in the middle 3. If we can cut an array into two puts, and call the element immediately left to the cut L, and right to the cut the R, we can find the median easily by average them 1. \[2 3 / 5 7\] -> median = \(3+5\)/2 2. \[2 3 \(4/4\) 5 7\] -> median = \(4 + 4\) / 2 = 4 1. The index can be listed as follow 1. \`\`\`text N Index of L / R 1 0 / 0 2 0 / 1 3 1 / 1
36
+
4 1 / 2
37
+
5 2 / 2 6 2 / 3 7 3 / 3 8 3 / 4
48
38
39
+
```text
49
40
The formula is derived as \(L + R\)/2 = \(A\[\(N-1\)/2\] + A\[N/2\]\)/2
50
41
51
42
2. For two arrays, we need to add some padding to make the index more consistent
4. We need to make sure L1 <= R1 && L1 <= R2 && L2 <= R1 && L2 <= R2
72
+
2. We need to make sure L1 <= R1 && L1 <= R2 && L2 <= R1 && L2 <= R2
80
73
1. Since arrays are sorted, we can drop L1 <= R1 and L2 <= R2 conditions, just focus on L1 <= R2 and L2 <= R1
81
74
2. If L1 > R2, we have too many large elements on left of A1, we need to move C1 to left or C2 to the right
82
75
3. If L2 > R1, we have too many large elements on left of A2, we need to move C2 to the left
83
-
5. Because C1 and C2 are mutually determinable, we can just pick one. However, it's much more practical to use the shorter array as the basis, since all cut position on it is possible median. On the longer array, the position too far to the left or right is simply impossible.
84
-
7. For instance, \[1\], \[2 3 4 5 6 7 8\]. 1. Corner cases are when C2 == 0 and C2 == 2N2, where we can insert a imaginary element A\[-1\] = INT\_MIN and A\[2N2\] = INT\_MAX
76
+
3. Because C1 and C2 are mutually determinable, we can just pick one. However, it's much more practical to use the shorter array as the basis, since all cut position on it is possible median. On the longer array, the position too far to the left or right is simply impossible.
77
+
78
+
5. For instance, \[1\], \[2 3 4 5 6 7 8\]. 1. Corner cases are when C2 == 0 and C2 == 2N2, where we can insert a imaginary element A\[-1\] = INT\_MIN and A\[2N2\] = INT\_MAX
Copy file name to clipboardExpand all lines: leetcode/array/088-merge-sorted-array.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
7
7
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
8
8
9
-
**Note:**
9
+
**Note:**
10
10
You may assume that nums1 has enough space \(size that is greater or equal to m + n\) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
0 commit comments