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-28
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-1
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.
Copy file name to clipboardExpand all lines: leetcode/binary-search/497-random-point-in-non-overlapping-rectangles.md
+2-3
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,7 @@
6
6
7
7
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles.
8
8
9
-
**Note:**
10
-
9
+
**Note:**
11
10
12
11
1. An integer point is a point that has integer coordinates.
13
12
2. A point on the perimeter of a rectangle is included in the space covered by the rectangles.
@@ -37,7 +36,7 @@ Output:
37
36
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]
38
37
```
39
38
40
-
**Explanation of Input Syntax:**
39
+
**Explanation of Input Syntax:**
41
40
42
41
The input is two lists: the subroutines called and their arguments. Solution's constructor has one argument, the array of rectangles rects. pick has no arguments. Arguments are always wrapped with a list, even if there aren't any.
Copy file name to clipboardExpand all lines: leetcode/binary-search/528-random-pick-with-weight.md
+2-4
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,7 @@
6
6
7
7
Given an array w of positive integers, where w\[i\] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.
8
8
9
-
**Note:**
10
-
9
+
**Note:**
11
10
12
11
1. 1 <= w.length <= 10000
13
12
2. 1 <= w\[i\]<= 10^5
@@ -31,8 +30,7 @@ Input:
31
30
Output: [null,0,1,1,1,0]
32
31
```
33
32
34
-
**Explanation of Input Syntax:**
35
-
33
+
**Explanation of Input Syntax:**
36
34
37
35
The input is two lists: the subroutines called and their arguments. Solution's constructor has one argument, the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren't any.
Copy file name to clipboardExpand all lines: leetcode/binary-search/710-random-pick-with-blacklist.md
+2-4
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,7 @@ Given a blacklist B containing unique integers from \[0, N\), write a function t
6
6
7
7
Optimize it such that it minimizes the call to system’s Math.random\(\).
8
8
9
-
**Note:**
10
-
9
+
**Note:**
11
10
12
11
1. 1 <= N <= 1000000000
13
12
2. 0 <= B.length < min\(100000, N\)
@@ -49,8 +48,7 @@ Input:
49
48
Output: [null,1,3,1]
50
49
```
51
50
52
-
**Explanation of Input Syntax:**
53
-
51
+
**Explanation of Input Syntax:**
54
52
55
53
The input is two lists: the subroutines called and their arguments. Solution's constructor has two arguments, N and the blacklist B. pick has no arguments. Arguments are always wrapped with a list, even if there aren't any.
0 commit comments