Skip to content

Commit e68376d

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent 96bb8ea commit e68376d

File tree

3 files changed

+262
-37
lines changed

3 files changed

+262
-37
lines changed

SUMMARY.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,42 +132,42 @@
132132
* [296-best-meeting-point](leetcode/math/296-best-meeting-point.md)
133133
* [313-super-ugly-number](leetcode/math/313-super-ugly-number.md)
134134
* [396-rotate-function](leetcode/math/396-rotate-function.md)
135-
* [007-reverse-integer](leetcode/math/007-reverse-integer.md)
136-
* [008-string-to-integer-atoi](leetcode/math/008-string-to-integer-atoi.md)
137-
* [009-palindrome-number](leetcode/math/009-palindrome-number.md)
138-
* [012-integer-to-roman](leetcode/math/012-integer-to-roman.md)
139-
* [013-roman-to-integer](leetcode/math/013-roman-to-integer.md)
140-
* [014-longest-common-prefix](leetcode/math/014-longest-common-prefix.md)
141-
* [029-divide-two-integers](leetcode/math/029-divide-two-integers.md)
142-
* [043-multiply-strings](leetcode/math/043-multiply-strings.md)
143-
* [050-powx-n](leetcode/math/050-powx-n.md)
144-
* [060-permutation-sequence](leetcode/math/060-permutation-sequence.md)
145-
* [067-add-binary](leetcode/math/067-add-binary.md)
146-
* [069-sqrtx](leetcode/math/069-sqrtx.md)
147-
* [168-excel-sheet-column-title](leetcode/math/168-excel-sheet-column-title.md)
148-
* [171-excel-sheet-column-number](leetcode/math/171-excel-sheet-column-number.md)
149-
* [223-rectangle-area](leetcode/math/223-rectangle-area.md)
150-
* [224-basic-calculator](leetcode/math/224-basic-calculator.md)
151-
* [247-strobogrammatic-number-ii](leetcode/math/247-strobogrammatic-number-ii.md)
152-
* [248-strobogrammatic-number-iii](leetcode/math/248-strobogrammatic-number-iii.md)
153-
* [258-add-digits](leetcode/math/258-add-digits.md)
154-
* [263-ugly-number](leetcode/math/263-ugly-number.md)
155-
* [264-ugly-number-ii](leetcode/math/264-ugly-number-ii.md)
156-
* [268-missing-number](leetcode/math/268-missing-number.md)
157-
* [273-integer-to-english-words](leetcode/math/273-integer-to-english-words.md)
158-
* [279-perfect-squares](leetcode/math/279-perfect-squares.md)
159-
* [319-bulb-switcher](leetcode/math/319-bulb-switcher.md)
160-
* [326-power-of-three](leetcode/math/326-power-of-three.md)
161-
* [335-self-crossing](leetcode/math/335-self-crossing.md)
162-
* [343-integer-break](leetcode/math/343-integer-break.md)
163-
* [357-count-numbers-with-unique-digits](leetcode/math/357-count-numbers-with-unique-digits.md)
164-
* [360-sort-transformed-array](leetcode/math/360-sort-transformed-array.md)
165-
* [365-water-and-jug-problem](leetcode/math/365-water-and-jug-problem.md)
166-
* [367-valid-perfect-square](leetcode/math/367-valid-perfect-square.md)
167-
* [368-largest-divisible-subset](leetcode/math/368-largest-divisible-subset.md)
168-
* [372-super-pow](leetcode/math/372-super-pow.md)
169-
* [397-integer-replacement](leetcode/math/397-integer-replacement.md)
170-
* [400-nth-digit](leetcode/math/400-nth-digit.md)
135+
* [007-reverse-integer](leetcode/math/007-reverse-integer.md)
136+
* [008-string-to-integer-atoi](leetcode/math/008-string-to-integer-atoi.md)
137+
* [009-palindrome-number](leetcode/math/009-palindrome-number.md)
138+
* [012-integer-to-roman](leetcode/math/012-integer-to-roman.md)
139+
* [013-roman-to-integer](leetcode/math/013-roman-to-integer.md)
140+
* [014-longest-common-prefix](leetcode/math/014-longest-common-prefix.md)
141+
* [029-divide-two-integers](leetcode/math/029-divide-two-integers.md)
142+
* [043-multiply-strings](leetcode/math/043-multiply-strings.md)
143+
* [050-powx-n](leetcode/math/050-powx-n.md)
144+
* [060-permutation-sequence](leetcode/math/060-permutation-sequence.md)
145+
* [067-add-binary](leetcode/math/067-add-binary.md)
146+
* [069-sqrtx](leetcode/math/069-sqrtx.md)
147+
* [168-excel-sheet-column-title](leetcode/math/168-excel-sheet-column-title.md)
148+
* [171-excel-sheet-column-number](leetcode/math/171-excel-sheet-column-number.md)
149+
* [223-rectangle-area](leetcode/math/223-rectangle-area.md)
150+
* [224-basic-calculator](leetcode/math/224-basic-calculator.md)
151+
* [247-strobogrammatic-number-ii](leetcode/math/247-strobogrammatic-number-ii.md)
152+
* [248-strobogrammatic-number-iii](leetcode/math/248-strobogrammatic-number-iii.md)
153+
* [258-add-digits](leetcode/math/258-add-digits.md)
154+
* [263-ugly-number](leetcode/math/263-ugly-number.md)
155+
* [264-ugly-number-ii](leetcode/math/264-ugly-number-ii.md)
156+
* [268-missing-number](leetcode/math/268-missing-number.md)
157+
* [273-integer-to-english-words](leetcode/math/273-integer-to-english-words.md)
158+
* [279-perfect-squares](leetcode/math/279-perfect-squares.md)
159+
* [319-bulb-switcher](leetcode/math/319-bulb-switcher.md)
160+
* [326-power-of-three](leetcode/math/326-power-of-three.md)
161+
* [335-self-crossing](leetcode/math/335-self-crossing.md)
162+
* [343-integer-break](leetcode/math/343-integer-break.md)
163+
* [357-count-numbers-with-unique-digits](leetcode/math/357-count-numbers-with-unique-digits.md)
164+
* [360-sort-transformed-array](leetcode/math/360-sort-transformed-array.md)
165+
* [365-water-and-jug-problem](leetcode/math/365-water-and-jug-problem.md)
166+
* [367-valid-perfect-square](leetcode/math/367-valid-perfect-square.md)
167+
* [368-largest-divisible-subset](leetcode/math/368-largest-divisible-subset.md)
168+
* [372-super-pow](leetcode/math/372-super-pow.md)
169+
* [397-integer-replacement](leetcode/math/397-integer-replacement.md)
170+
* [400-nth-digit](leetcode/math/400-nth-digit.md)
171171
* [two-pointers](leetcode/two-pointers/README.md)
172172
* [028-implement-strstr](leetcode/two-pointers/028-implement-strstr.md)
173173
* [125-valid-palindrome](leetcode/two-pointers/125-valid-palindrome.md)
@@ -260,6 +260,7 @@
260260
* [744-find-smallest-letter-greater-than-target](leetcode/binary-search/744-find-smallest-letter-greater-than-target.md)
261261
* [774-minimize-max-distance-to-gas-station](leetcode/binary-search/774-minimize-max-distance-to-gas-station.md)
262262
* [778-swim-in-rising-water](leetcode/binary-search/778-swim-in-rising-water.md)
263+
* [786-k-th-smallest-prime-fraction](leetcode/binary-search/untitled.md)
263264
* [dynamic-programing](leetcode/dynamic-programing/README.md)
264265
* [174-dungeon-game](leetcode/dynamic-programing/174-dungeon-game.md)
265266
* [bit-manipulation](leetcode/bit-manipulation-1/README.md)

leetcode/binary-search/778-swim-in-rising-water.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,159 @@ We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
4646

4747
## Thought Process {#thought-process}
4848

49-
1. asd
49+
1. Binary Search and DFS
50+
1. Search between 0 and N \* N - 1 using binary search
51+
2. We stop when at a particular time, t, that the bottom is reached
52+
3. We create a DFS function reachBottom\(grid, t, N, visit, i, j\), where t is the candidate and visit is boolean array to avoid repeated check
53+
4. Time complexity O\(log\(N\) \* N^2\)
54+
5. Space complexity O\(N^2\)
55+
2. Union Find
56+
1. Union find structure will be particularly useful for finding if two points are connected
57+
2. We start putting each grid and its flattened index in the map
58+
3. Use while loop to check 0 and N \* N - 1 is connected
59+
4. If not, we increase the water depth and get the respective flattened index out of map and connect its neighbors if the depth is greater than theirs
60+
5. If yes, we can simply return the depth
61+
6. Time complexity O\(N^2\)???
62+
7. Space complexity O\(N^2\)
63+
3. Dijkstra's Search
64+
1. Create a node to store val and respective location i
65+
2. Use heap to store the nodes based on its val
66+
3. Every time, we poll the min node out and compared node's val with our current answer, res, where the max of two is our new answer
67+
4. If we have reach the right bottom corner, our search is done
68+
5. Else, we need to add current node's unvisited neighbors to the heap
69+
6. Time complexity O\(N^2log\(N\)\)
70+
7. Space complexity O\(N^2\)
5071

5172
## Solution
5273

5374
```java
75+
class Solution {
76+
public int swimInWater(int[][] grid) {
77+
int N = grid.length;
78+
int lo = 0, hi = N * N - 1;
79+
while (lo < hi) {
80+
int mi = lo + (hi - lo) / 2;
81+
boolean[][] visit = new boolean[N][N];
82+
if (reachBottom(grid, mi, N, visit, 0, 0)) hi = mi;
83+
else lo = mi + 1;
84+
}
85+
return lo;
86+
}
87+
88+
private boolean reachBottom(int[][] grid, int t, int N, boolean[][] visit, int i, int j) {
89+
if (i < 0 || i >= N || j < 0 || j >= N || visit[i][j] || grid[i][j] > t) return false;
90+
visit[i][j] = true;
91+
if (i == N - 1 && j == N - 1) return true;
92+
else return reachBottom(grid, t, N, visit, i + 1, j) || reachBottom(grid, t, N, visit, i - 1, j) || reachBottom(grid, t, N, visit, i, j - 1) || reachBottom(grid, t, N, visit, i, j + 1);
93+
}
94+
}
95+
```
5496

97+
```java
98+
class Solution {
99+
private int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
100+
101+
private class UF {
102+
int[] ids;
103+
int[] size;
104+
public UF(int n) {
105+
ids = new int[n];
106+
size = new int[n];
107+
for (int i = 0; i < n; i++) {
108+
ids[i] = i;
109+
size[i] = 1;
110+
}
111+
}
112+
113+
public boolean isConnected(int i, int j) {
114+
return find(i) == find(j);
115+
}
116+
117+
private int find(int i) {
118+
int root = i;
119+
while (ids[root] != root) root = ids[root];
120+
while (i != root) {
121+
int next = ids[i];
122+
ids[i] = root;
123+
i = next;
124+
}
125+
return ids[i];
126+
}
127+
128+
public void union(int i, int j) {
129+
int rootI = find(i), rootJ = find(j);
130+
if (rootI == rootJ) return;
131+
if (size[rootI] < size[rootJ]) {
132+
ids[rootI] = rootJ;
133+
size[rootJ] += size[rootI];
134+
} else {
135+
ids[rootJ] = rootI;
136+
size[rootI] += size[rootJ];
137+
}
138+
}
139+
}
140+
141+
public int swimInWater(int[][] grid) {
142+
int N = grid.length;
143+
Map<Integer, Integer> map = new HashMap<>();
144+
for (int i = 0; i < N; i++) {
145+
for (int j = 0; j < N; j++) {
146+
map.put(grid[i][j], i * N + j);
147+
}
148+
}
149+
UF uf = new UF(N * N);
150+
int res = 0;
151+
while (!uf.isConnected(0, N * N - 1)) {
152+
res++;
153+
int k = map.get(res);
154+
int i = k / N, j = k % N;
155+
for (int[] dir : dirs) {
156+
int x = i + dir[0], y = j + dir[1];
157+
if (x >= 0 && x < N && y >= 0 && y < N && grid[i][j] > grid[x][y]) {
158+
uf.union(k, x * N + y);
159+
}
160+
}
161+
}
162+
return res;
163+
}
164+
}
165+
```
166+
167+
```java
168+
class Solution {
169+
private class Node{
170+
int v, i, j;
171+
public Node(int v, int i, int j) {
172+
this.v = v;
173+
this.i = i;
174+
this.j = j;
175+
}
176+
}
177+
178+
private int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
179+
180+
public int swimInWater(int[][] grid) {
181+
int N = grid.length;
182+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.v - b.v);
183+
pq.offer(new Node(grid[0][0], 0, 0));
184+
int res = grid[0][0];
185+
boolean[][] visited = new boolean[N][N];
186+
while (!pq.isEmpty()) {
187+
Node cur = pq.poll();
188+
res = Math.max(cur.v, res);
189+
if (cur.i == N - 1 && cur.j == N - 1) break;
190+
for (int[] dir : dirs) {
191+
int x = cur.i + dir[0];
192+
int y = cur.j + dir[1];
193+
if (x >= 0 && x < N && y >= 0 && y < N && !visited[x][y]) {
194+
pq.offer(new Node(grid[x][y], x, y));
195+
visited[x][y] = true;
196+
}
197+
}
198+
}
199+
return res;
200+
}
201+
}
55202
```
56203

57204
## Additional {#additional}

leetcode/binary-search/untitled.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 786-k-th-smallest-prime-fraction
2+
3+
## Question {#question}
4+
5+
A sorted list A contains 1, plus some number of primes. Then, for every p &lt; q in the list, we consider the fraction p/q.
6+
7+
What is the K-th smallest fraction considered? Return your answer as an array of ints, where answer\[0\] = p and answer\[1\] = q.
8+
9+
**Example:**
10+
11+
```text
12+
Input: A = [1, 2, 3, 5], K = 3
13+
Output: [2, 5]
14+
Explanation:
15+
The fractions to be considered in sorted order are:
16+
1/5, 1/3, 2/5, 1/2, 3/5, 2/3.
17+
The third fraction is 2/5.
18+
19+
Input: A = [1, 7], K = 1
20+
Output: [1, 7]
21+
```
22+
23+
**Note:**
24+
25+
* A will have length between 2 and 2000.
26+
* Each A\[i\] will be between 1 and 30000.
27+
* K will be between 1 and A.length \* \(A.length - 1\) / 2.
28+
29+
## Thought Process {#thought-process}
30+
31+
1. MinHeap - Brute Force \(TLE\)
32+
1. Save the dividend p and divisor q into a node
33+
2. Use priority queue to poll up a node base on the quotient, which = p / q
34+
3. The Kth node on the heap will the our answer
35+
4. Time complexity O\(n^2log\(n\)\)
36+
5. Space complexity O\(n^2\)
37+
2. MaxHeap - Size Control \(TLE\)
38+
1. Use max heap we can improve the performance
39+
2. We monitor the size of heap, and poll out biggest one if the heap size is greater than K
40+
3. Time complexity O\(n^2log\(K\)\)
41+
4. Space complexity O\(K\)
42+
3. MinHeap
43+
1. Instead of checking all the combinations, we smartly add the next smallest quotient
44+
2. We create the node to store the indices of dividend and divisor
45+
3. Every time, we poll out a min node, we slide the divisor forward until the dividend's next number
46+
4. Time complexity O\(nlog\(n\)\)
47+
5. Space complexity O\(n\)
48+
4. asdsad
49+
50+
## Solution
51+
52+
```java
53+
class Solution {
54+
public class Node {
55+
int p, q;
56+
Node (int p, int q) {
57+
this.p = p;
58+
this.q = q;
59+
}
60+
}
61+
62+
public int[] kthSmallestPrimeFraction(int[] A, int K) {
63+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.p * 1.0 / a.q > b.p * 1.0 / b.q ? 1 : -1);
64+
for (int i = 0; i < A.length - 1; i++) {
65+
for (int j = i + 1; j < A.length; j++) {
66+
pq.offer(new Node(A[i], A[j]));
67+
}
68+
}
69+
while (--K > 0) pq.poll();
70+
int[] res = {pq.peek().p, pq.peek().q};
71+
return res;
72+
}
73+
}
74+
```
75+
76+
## Additional {#additional}
77+

0 commit comments

Comments
 (0)