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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 13 deletions
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

Lines changed: 0 additions & 4 deletions
This file was deleted.

leetcode/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# leetcode
22

3+
4+

leetcode/array-1.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 22 additions & 28 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# array
22

3+
4+

leetcode/binary-search-1.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)