Skip to content

Commit 5bf63af

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 52 pages modified
1 parent 76c19d5 commit 5bf63af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+80
-249
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
---
2-
description: This is my notes for doing Leetcode problem.
3-
---
4-
51
# README
62

73

SUMMARY.md

-13
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,15 @@
259259
* [328-odd-even-linked-list](leetcode/linked-list/328-odd-even-linked-list.md)
260260
* [369-plus-one-linked-list](leetcode/linked-list/369-plus-one-linked-list.md)
261261
* [109-convert-sorted-list-to-binary-search-tree](leetcode/linked-list/109-convert-sorted-list-to-binary-search-tree.md)
262-
* [divide-and-conquer](leetcode/divide-and-conquer.md)
263-
* [math](leetcode/math-1.md)
264-
* [array](leetcode/array-1.md)
265-
* [binary-search](leetcode/binary-search-1.md)
266-
* [linked-list](leetcode/linked-list-1.md)
267-
* [bit-manipulation](leetcode/bit-manipulation.md)
268-
* [two-pointers](leetcode/two-pointers-1.md)
269262
* [bit-manipulation](leetcode/bit-manipulation-1/README.md)
270263
* [260-single-number-iii](leetcode/bit-manipulation-1/260-single-number-iii.md)
271264
* [137-single-number-ii](leetcode/bit-manipulation-1/137-single-number-ii.md)
272265
* [dynamic-programing](leetcode/dynamic-programing/README.md)
273266
* [174-dungeon-game](leetcode/dynamic-programing/174-dungeon-game.md)
274-
* [string](leetcode/string-1.md)
275-
* [hash-table](leetcode/hash-table-1.md)
276-
* [dynamic-programing](leetcode/dynamic-programing-1.md)
277267
* [other](other/README.md)
278268
* [euclidean-algorithm](other/euclidean-algorithm.md)
279269
* [rabin-karp-rolling-hash](other/rabin-karp-rolling-hash.md)
280270
* [summation-identities](other/summation-identities.md)
281-
* [template](template.md)
282-
* [LeetCode](leetcode-1.md)
283-
* [other](other-1.md)
284271
* [template](template-1/README.md)
285272
* [leetcode](template-1/leetcode.md)
286273

leetcode-1.md

-4
This file was deleted.

leetcode/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# leetcode
22

3+
4+

leetcode/array-1.md

-16
This file was deleted.

leetcode/array/004-median-of-two-sorted-arrays.md

+22-28
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,11 @@ The median is (2 + 3)/2 = 2.5
3232
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
3333
2. Time complexity will be O\(m+ n\)
3434
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
4838

39+
```text
4940
The formula is derived as \(L + R\)/2 = \(A\[\(N-1\)/2\] + A\[N/2\]\)/2
5041
5142
2. For two arrays, we need to add some padding to make the index more consistent
@@ -55,33 +46,36 @@ The median is (2 + 3)/2 = 2.5
5546
5647
[6 9 11 13 18]-> [# 6 # 9 # 11 # 13 # 18 #] (N = 5)
5748
position index 0 1 2 3 4 5 6 7 8 9 10 (N_Position = 11)
58-
```
49+
```
5950

60-
There are always 2N + 1 position and the cut is always at N position
51+
There are always 2N + 1 position and the cut is always at N position
6152

62-
3. Observe that index\(L\) = \(N - 1\) / 2, and index\(R\) = N / 2
63-
4. There are 2N1 + 2N2 + 2 position altogether. Because we made two cuts on two arrays, there should be N1 + N2 on each side
64-
5. If we made the cut C2 = K in A2, then the C1 = N1 + N2 - K.
65-
6. Then we have two L's and two R's
66-
1. ```text
67-
[# 1 # 2 # 3 # (4/4) # 5 #]
53+
1. Observe that index\(L\) = \(N - 1\) / 2, and index\(R\) = N / 2
54+
2. There are 2N1 + 2N2 + 2 position altogether. Because we made two cuts on two arrays, there should be N1 + N2 on each side
55+
3. If we made the cut C2 = K in A2, then the C1 = N1 + N2 - K.
56+
4. Then we have two L's and two R's 1. \`\`\`text \[\# 1 \# 2 \# 3 \# \(4/4\) \# 5 \#\]
6857

69-
[# 1 / 1 # 1 # 1 #]
70-
```
58+
```text
59+
[# 1 / 1 # 1 # 1 #]
60+
```
61+
62+
```text
7163
2. ```text
7264
L1 = A1[(C1-1)/2]; R1 = A1[C1/2];
7365
L2 = A2[(C2-1)/2]; R2 = A2[C2/2];
74-
```
75-
3. ```text
66+
```
67+
68+
1. ```text
7669
L1 = A1[(7-1)/2] = A1[3] = 4; R1 = A1[7/2] = A1[3] = 4;
7770
L2 = A2[(2-1)/2] = A2[0] = 1; R2 = A1[2/2] = A1[1] = 1;
7871
```
79-
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
8073
1. Since arrays are sorted, we can drop L1 <= R1 and L2 <= R2 conditions, just focus on L1 <= R2 and L2 <= R1
8174
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
8275
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
8579
8680
## Solution
8781

leetcode/array/062-unique-paths.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ How many possible unique paths are there?
2323
2. Now for two dimensional array, the ways to get there will be way\[left\] and way\[top\]
2424
3. Time complexity O\(mn\)
2525
4. Space complexity O\(mn\)
26-
2. DP - Optimized Space
27-
1. W
26+
2. DP - Optimized Space 1. W
2827

29-
e can save the space by using one dimensional array
28+
e can save the space by using one dimensional array
29+
30+
1. We can choose the smaller one to save the space, for now we just stick with columns n
31+
2. Time complexity O\(mn\)
32+
3. Space complexity O\(n\)
3033

31-
2. We can choose the smaller one to save the space, for now we just stick with columns n
32-
3. Time complexity O\(mn\)
33-
4. Space complexity O\(n\)
3434
3. Math formula
3535
1. There are total of m + n - 2 steps, m -1 down and n - 1 right
3636
2. This question is essentially asking how many ways we can get by performing m - 1 down steps out of total m + n - 2 moves

leetcode/array/088-merge-sorted-array.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
88

9-
**Note:**
9+
**Note:**
1010
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.
1111

1212
**Example:**

leetcode/array/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# array
22

3+
4+

leetcode/binary-search-1.md

-24
This file was deleted.

leetcode/binary-search/230-kth-smallest-element-in-a-bst.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
88

9-
**Note:**
9+
**Note:**
1010

1111
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
1212

leetcode/binary-search/363-max-sum-of-rectangle-no-larger-than-k.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ k = 2
1818

1919
The answer is 2. Because the sum of rectangle \[\[0, 1\], \[-2, 3\]\] is 2 and 2 is the max number no larger than k \(k = 2\).
2020

21-
**Note:**
21+
**Note:**
2222

2323
1. The rectangle inside the matrix must have an area > 0.
2424
2. What if the number of rows is much larger than the number of columns?

leetcode/binary-search/497-random-point-in-non-overlapping-rectangles.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
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.
88

9-
**Note:**
10-
9+
**Note:**
1110

1211
1. An integer point is a point that has integer coordinates.
1312
2. A point on the perimeter of a rectangle is included in the space covered by the rectangles.
@@ -37,7 +36,7 @@ Output:
3736
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]
3837
```
3938

40-
**Explanation of Input Syntax:**
39+
**Explanation of Input Syntax:**
4140

4241
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.
4342

leetcode/binary-search/528-random-pick-with-weight.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
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.
88

9-
**Note:**
10-
9+
**Note:**
1110

1211
1. 1 <= w.length <= 10000
1312
2. 1 <= w\[i\] <= 10^5
@@ -31,8 +30,7 @@ Input:
3130
Output: [null,0,1,1,1,0]
3231
```
3332

34-
**Explanation of Input Syntax:**
35-
33+
**Explanation of Input Syntax:**
3634

3735
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.
3836

leetcode/binary-search/644-maximum-average-subarray-ii.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ when length is 6, maximum average value is 9.16667.
1717
Thus return 12.75.
1818
```
1919

20-
**Note:**
21-
20+
**Note:**
2221

2322
1. 1 <= k <= n <= 10,000.
2423
2. Elements of the given array will be in range \[-10,000, 10,000\].

leetcode/binary-search/658-find-k-closest-elements.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ Input: [1,2,3,4,5], k=4, x=-1
2020
Output: [1,2,3,4]
2121
```
2222

23-
**Note:**
24-
23+
**Note:**
2524

2625
1. The value k is positive and will always be smaller than the length of the sorted array.
2726
2. Length of the given array is positive and will not exceed 104

leetcode/binary-search/668-kth-smallest-number-in-multiplication-table.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ The Multiplication Table:
3535
The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).
3636
```
3737

38-
**Note:**
39-
38+
**Note:**
4039

4140
1. The m and n will be in the range \[1, 30000\].
4241
2. The k will be in the range \[1, m \* n\]

leetcode/binary-search/702-search-in-a-sorted-array-of-unknown-size.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Output: -1
2424
Explanation: 2 does not exist in nums so return -1
2525
```
2626

27-
**Note:**
28-
27+
**Note:**
2928

3029
1. You may assume that all elements in the array are unique.
3130
2. The value of each element in the array will be in the range \[-9999, 9999\].

leetcode/binary-search/704-binary-search.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Output: -1
2222
Explanation: 2 does not exist in nums so return -1
2323
```
2424

25-
**Note:**
25+
**Note:**
2626

2727
1. You may assume that all elements in nums are unique.
2828
2. n will be in the range \[1, 10000\].

leetcode/binary-search/710-random-pick-with-blacklist.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Given a blacklist B containing unique integers from \[0, N\), write a function t
66

77
Optimize it such that it minimizes the call to system’s Math.random\(\).
88

9-
**Note:**
10-
9+
**Note:**
1110

1211
1. 1 <= N <= 1000000000
1312
2. 0 <= B.length < min\(100000, N\)
@@ -49,8 +48,7 @@ Input:
4948
Output: [null,1,3,1]
5049
```
5150

52-
**Explanation of Input Syntax:**
53-
51+
**Explanation of Input Syntax:**
5452

5553
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.
5654

leetcode/binary-search/718-maximum-length-of-repeated-subarray.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ Explanation:
1717
The repeated subarray with maximum length is [3, 2, 1].
1818
```
1919

20-
**Note:**
21-
20+
**Note:**
2221

2322
1. 1 <= len\(A\), len\(B\) <= 1000
2423
2. 0 <= A\[i\], B\[i\] < 100

leetcode/binary-search/719-find-k-th-smallest-pair-distance.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ Here are all the pairs:
2121
Then the 1st smallest distance pair is (1,1), and its distance is 0.
2222
```
2323

24-
**Note:**
25-
24+
**Note:**
2625

2726
1. 2 <= len\(nums\) <= 10000.
2827
2. 0 <= nums\[i\] < 1000000.

leetcode/binary-search/744-find-smallest-letter-greater-than-target.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ target = "k"
4242
Output: "c"
4343
```
4444

45-
**Note:**
46-
45+
**Note:**
4746

4847
1. letters has a length in range \[2, 10000\].
4948
2. letters consists of lowercase letters, and contains at least 2 unique letters.

leetcode/binary-search/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# binary-search
22

3+
4+

leetcode/bit-manipulation-1/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# bit-manipulation
22

3+
4+

leetcode/bit-manipulation.md

-6
This file was deleted.

leetcode/divide-and-conquer.md

-4
This file was deleted.

leetcode/dynamic-programing-1.md

-4
This file was deleted.

leetcode/dynamic-programing/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# dynamic-programing
22

3+
4+

leetcode/hash-table-1.md

-4
This file was deleted.

leetcode/hash-table/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# hash-table
22

3+
4+

0 commit comments

Comments
 (0)