Skip to content

Commit 76c19d5

Browse files
committed
Revert "GitBook: [master] 232 pages modified"
This reverts commit 819e306.
1 parent 819e306 commit 76c19d5

File tree

232 files changed

+16785
-47
lines changed

Some content is hidden

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

232 files changed

+16785
-47
lines changed

SUMMARY.md

Lines changed: 225 additions & 0 deletions
Large diffs are not rendered by default.

leetcode/array-1.md

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

33
* Two sum, k sum
4-
* [001-Two Sum]()
5-
* [015-3Sum]()
6-
* [016-3Sum Closest ]()
7-
* [018-4Sum]()
4+
* [001-Two Sum](array/001-two-sum.md)
5+
* [015-3Sum](array/015-3sum.md)
6+
* [016-3Sum Closest ](array/016-3sum-closest.md)
7+
* [018-4Sum](array/018-4sum.md)
88
* Backtrack
9-
* [039-Combination Sum]()
10-
* [040-Combination Sum II]()
9+
* [039-Combination Sum](array/039-combination-sum.md)
10+
* [040-Combination Sum II](array/040-combination-sum-ii.md)
1111
* Dynamic Programing
12-
* [118-Pascal's Triangle]()
13-
* [119-Pascal's Triangle II]()
14-
* [120-Triangle]()
12+
* [118-Pascal's Triangle](array/118-pascals-triangle.md)
13+
* [119-Pascal's Triangle II](array/119-pascals-triangle-ii.md)
14+
* [120-Triangle](array/120-triangle.md)
1515
* asdasd
1616

leetcode/array/001-two-sum.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 001-two-sum
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/two-sum/description/](https://leetcode.com/problems/two-sum/description/)
6+
7+
Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.
8+
9+
You may assume that each input would have **exactly** one solution, and you may not use the same element twice.
10+
11+
**Example:**
12+
13+
```text
14+
Given nums = [2, 7, 11, 15], target = 9,
15+
16+
Because nums[0] + nums[1] = 2 + 7 = 9,
17+
return [0, 1].
18+
```
19+
20+
## Thought Process {#thought-process}
21+
22+
1. Brute Force
23+
1. Use every element as an anchor and search its right element
24+
2. The time complexity is O\(n^2\) = \(n - 1\) + \(n - 2\) + ... + 1
25+
3. The space complexity is O\(1\)
26+
2. Optimal \(Hash Map\)
27+
1. Leverage the power of hashmap and store the number and its index in the map
28+
2. When the map contain target - curNum, we know the search is complete
29+
3. The time complexity is O\(n\)
30+
4. The space complexity is O\(n\)
31+
32+
## Solution {#solution}
33+
34+
```java
35+
class Solution {
36+
public int[] twoSum(int[] nums, int target) {
37+
if (nums == null) return null;
38+
int[] result = new int[2];
39+
Map<Integer, Integer> map = new HashMap<>();
40+
for (int i = 0; i < nums.length; i++) {
41+
if (map.containsKey(target - nums[i])) {
42+
result[0] = map.get(target - nums[i]);
43+
result[1] = i;
44+
return result;
45+
}
46+
map.put(nums[i], i);
47+
}
48+
return result;
49+
}
50+
}
51+
```
52+
53+
## Additional {#additional}
54+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# 004-median-of-two-sorted-arrays
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/median-of-two-sorted-arrays/description/](https://leetcode.com/problems/median-of-two-sorted-arrays/description/)
6+
7+
There are two sorted arrays **nums1** and **nums2** of size m and n respectively.
8+
9+
Find the median of the two sorted arrays. The overall run time complexity should be O\(log \(m+n\)\).
10+
11+
**Example 1:**
12+
13+
```text
14+
nums1 = [1, 3]
15+
nums2 = [2]
16+
17+
The median is 2.0
18+
```
19+
20+
**Example 2:**
21+
22+
```text
23+
nums1 = [1, 2]
24+
nums2 = [3, 4]
25+
26+
The median is (2 + 3)/2 = 2.5
27+
```
28+
29+
## Thought Process {#thought-process}
30+
31+
1. Brute Force
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+
2. Time complexity will be O\(m+ n\)
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\] -&gt; median = \(3+5\)/2 2. \[2 3 \(4/4\) 5 7\] -&gt; 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+
```
48+
49+
The formula is derived as \(L + R\)/2 = \(A\[\(N-1\)/2\] + A\[N/2\]\)/2
50+
51+
2. For two arrays, we need to add some padding to make the index more consistent
52+
2. ```text
53+
[6 9 13 18] -> [# 6 # 9 # 13 # 18 #] (N = 4)
54+
position index 0 1 2 3 4 5 6 7 8 (N_Position = 9)
55+
56+
[6 9 11 13 18]-> [# 6 # 9 # 11 # 13 # 18 #] (N = 5)
57+
position index 0 1 2 3 4 5 6 7 8 9 10 (N_Position = 11)
58+
```
59+
60+
There are always 2N + 1 position and the cut is always at N position
61+
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 #]
68+
69+
[# 1 / 1 # 1 # 1 #]
70+
```
71+
2. ```text
72+
L1 = A1[(C1-1)/2]; R1 = A1[C1/2];
73+
L2 = A2[(C2-1)/2]; R2 = A2[C2/2];
74+
```
75+
3. ```text
76+
L1 = A1[(7-1)/2] = A1[3] = 4; R1 = A1[7/2] = A1[3] = 4;
77+
L2 = A2[(2-1)/2] = A2[0] = 1; R2 = A1[2/2] = A1[1] = 1;
78+
```
79+
4. We need to make sure L1 &lt;= R1 && L1 &lt;= R2 && L2 &lt;= R1 && L2 &lt;= R2
80+
1. Since arrays are sorted, we can drop L1 &lt;= R1 and L2 &lt;= R2 conditions, just focus on L1 &lt;= R2 and L2 &lt;= R1
81+
2. If L1 &gt; R2, we have too many large elements on left of A1, we need to move C1 to left or C2 to the right
82+
3. If L2 &gt; 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
85+
86+
## Solution
87+
88+
Specifically address the odd length, return the min of right side, since right side has more elements.
89+
90+
```java
91+
class Solution {
92+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
93+
int N1 = nums1.length, N2 = nums2.length;
94+
// We want to have smaller array as the second input
95+
if (N1 < N2) return findMedianSortedArrays(nums2, nums1);
96+
// The reason is that if we use pointers on the larger array,
97+
// the index of smaller array might be larger than its length.
98+
// The indice of smaller array are all potential cut.
99+
// @param halfLen store how many element we need to have on each side
100+
int lo = 0, hi = N2, halfLen = (N1 + N2) / 2;
101+
double L1 = Double.NaN, L2 = Double.NaN, R1 = Double.NaN, R2 = Double.NaN;
102+
while (lo <= hi) {
103+
// These two variables are the indice of R1 and R2
104+
// If the total length is odd, we will have an extra
105+
// element on the right.
106+
int mid2 = lo + (hi - lo) / 2;
107+
int mid1 = halfLen - mid2;
108+
L1 = mid1 == 0 ? Integer.MIN_VALUE : nums1[mid1 - 1];
109+
L2 = mid2 == 0 ? Integer.MIN_VALUE : nums2[mid2 - 1];
110+
R1 = mid1 == N1 ? Integer.MAX_VALUE : nums1[mid1];
111+
R2 = mid2 == N2 ? Integer.MAX_VALUE : nums2[mid2];
112+
if (L1 > R2) lo++;
113+
else if (L2 > R1) hi--;
114+
else {
115+
if ((N1 + N2) % 2 == 1) return Math.min(R1, R2);
116+
else break;
117+
}
118+
}
119+
return (Math.max(L1, L2) + Math.min(R1, R2)) / 2;
120+
}
121+
}
122+
```
123+
124+
With position padding, the code is slightly simpler.
125+
126+
```java
127+
class Solution {
128+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
129+
int N1 = nums1.length, N2 = nums2.length;
130+
// We want to have smaller array as the second input
131+
if (N1 < N2) return findMedianSortedArrays(nums2, nums1);
132+
// The reason is that if we use pointers on the larger array,
133+
// the index of smaller array might be larger than its length.
134+
// The indice of smaller array are all potential cut
135+
int lo = 0, hi = 2 * N2;
136+
double L1 = Double.NaN, L2 = Double.NaN, R1 = Double.NaN, R2 = Double.NaN;
137+
while (lo <= hi) {
138+
int mid2 = lo + (hi - lo) / 2;
139+
int mid1 = N1 + N2 - mid2;
140+
L1 = mid1 == 0 ? Integer.MIN_VALUE : nums1[(mid1 - 1)/2];
141+
L2 = mid2 == 0 ? Integer.MIN_VALUE : nums2[(mid2 - 1)/2];
142+
R1 = mid1 == 2 * N1 ? Integer.MAX_VALUE : nums1[mid1/2];
143+
R2 = mid2 == 2 * N2 ? Integer.MAX_VALUE :nums2[mid2/2];
144+
if (L1 > R2) lo = mid2 + 1;
145+
else if (L2 > R1) hi = mid2 - 1;
146+
else break;
147+
}
148+
return (Math.max(L1, L2) + Math.min(R1, R2)) / 2;
149+
}
150+
}
151+
```
152+
153+
## Additional {#additional}
154+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 011-container-with-most-water
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/container-with-most-water/description/](https://leetcode.com/problems/container-with-most-water/description/)
6+
7+
Given non-negative integers a1,a2, ...,an, where each represents a point at coordinate \(i,ai\).n vertical lines are drawn such that the two endpoints of line i is at \(i,ai\) and \(i, 0\). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
8+
9+
Note: You may not slant the container and n is at least 2.
10+
11+
**Example:**
12+
13+
![](../../.gitbook/assets/011.png)
14+
15+
## Thought Process {#thought-process}
16+
17+
1. Brute Force
18+
1. Start with each vertical line as the container's left boundary
19+
2. Pick every one of the remaining vertical line as the right boundary
20+
3. Volume = \(x2 - x1\) \* min\(y2, y1\)
21+
4. Time complexity is O\(n\)
22+
5. Space complexity is O\(1\)
23+
2. Two pointers
24+
1. Start with pointer at left and right, this could potentially by our largest container because the widest length
25+
2. We compute the volume same way as before
26+
3. Because only the larger height will be most useful in getting larger volume, we shrink the side that has lesser height
27+
4. Time complexity is O\(n\)
28+
5. Space complexity is O\(1\)
29+
30+
## Solution
31+
32+
```java
33+
class Solution {
34+
public int maxArea(int[] height) {
35+
int maxArea = 0, l = 0, r = height.length - 1;
36+
while (l < r) {
37+
maxArea = Math.max(maxArea, Math.min(height[l], height[r]) * (r - l));
38+
if (height[l] > height[r]) r--;
39+
else l++;
40+
}
41+
return maxArea;
42+
}
43+
}
44+
```
45+
46+
## Additional {#additional}
47+

leetcode/array/015-3sum.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 015-3sum
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/3sum/description/](https://leetcode.com/problems/3sum/description/)
6+
7+
Given an array S of n integers, are there elements a, b, c in S such that a+b+c= 0? Find all unique triplets in the array which gives the sum of zero.
8+
9+
**Note:**The solution set must not contain duplicate triplets.
10+
11+
**Example:**
12+
13+
```text
14+
For example, given array S = [-1, 0, 1, 2, -1, -4],
15+
16+
A solution set is:
17+
[
18+
[-1, 0, 1],
19+
[-1, -1, 2]
20+
]
21+
```
22+
23+
## Thought Process {#thought-process}
24+
25+
1. Brute force
26+
1. Triple loop and use set to eliminate the duplicates by sorting the solution
27+
2. Time complexity is O\(n^3\)
28+
3. Space complexity is O\(1\)
29+
2. Two Pointers
30+
1. Sort the array
31+
2. Loop through the elements, if the current element is equal to the last element, we skip it to avoid duplicate solution
32+
3. Set the pointer to the next element and the last element of remaining elements
33+
4. Time complexity is O\(n^2\)
34+
5. Space complexity is O\(1\)
35+
36+
## Solution
37+
38+
```java
39+
class Solution {
40+
public List<List<Integer>> threeSum(int[] nums) {
41+
List<List<Integer>> result = new ArrayList<>();
42+
if(nums == null || nums.length < 3) return result;
43+
Arrays.sort(nums);
44+
for(int i =0; i< nums.length - 2; i++){
45+
if(nums[i] > 0) break;
46+
if(i > 0 && nums[i] == nums[i-1]) continue;
47+
int lo = i + 1;
48+
int hi = nums.length - 1;
49+
while(lo < hi){
50+
int sum = nums[i] + nums[lo] + nums[hi];
51+
if(sum == 0){
52+
List<Integer> sol = new ArrayList<>();
53+
sol.add(nums[i]);
54+
sol.add(nums[lo]);
55+
sol.add(nums[hi]);
56+
result.add(sol);
57+
while(lo < hi && nums[lo] == nums[lo+1]) lo++;
58+
while(lo < hi && nums[hi] == nums[hi-1]) hi--;
59+
lo++;
60+
hi--;
61+
} else if(sum < 0){
62+
lo++;
63+
} else{
64+
hi--;
65+
}
66+
}
67+
}
68+
return result;
69+
}
70+
}
71+
```
72+
73+
## Additional {#additional}
74+

0 commit comments

Comments
 (0)