Skip to content

Commit a6c7b26

Browse files
author
Botao Xiao
committed
[Function add]:1. Add leetcode solutions with tag binary search.
1 parent 1d3f1d4 commit a6c7b26

24 files changed

+204
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## 378. Kth Smallest Element in a Sorted Matrix
2+
3+
### Question
4+
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
5+
6+
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
7+
8+
```
9+
Example:
10+
11+
matrix = [
12+
[ 1, 5, 9],
13+
[10, 11, 13],
14+
[12, 13, 15]
15+
],
16+
k = 8,
17+
18+
return 13.
19+
```
20+
21+
Note:
22+
1. You may assume k is always valid, 1 ≤ k ≤ n2.
23+
24+
### Solution
25+
* Method 1: PriorityQueue O(NlgN + K)
26+
```Java
27+
class Solution {
28+
public int kthSmallest(int[][] matrix, int k) {
29+
PriorityQueue<Integer> pq = new PriorityQueue<>();
30+
for(int[] mat : matrix)
31+
for(int m : mat)
32+
pq.offer(m);
33+
while(--k != 0)
34+
pq.poll();
35+
return pq.peek();
36+
}
37+
}
38+
```
39+
40+
* Method 2: binary search
41+
```Java
42+
class Solution {
43+
public int kthSmallest(int[][] matrix, int k) {
44+
int n = matrix.length;
45+
int low = matrix[0][0], high = matrix[n - 1][n - 1];
46+
while(low <= high){
47+
int mid = low + (high - low) / 2;
48+
int count = getLowerNum(matrix, mid);
49+
if(count < k) low = mid + 1;
50+
else high = mid - 1;
51+
}
52+
return low;
53+
}
54+
private int getLowerNum(int[][] matrix, int num){
55+
int n = matrix.length;
56+
int row = n - 1, col = 0;
57+
int res = 0;
58+
while(row >= 0 && col < n){
59+
if(matrix[row][col] > num)
60+
row--;
61+
else{
62+
res += row + 1;
63+
col++;
64+
}
65+
}
66+
return res;
67+
}
68+
}
69+
```

leetcode/429. N-ary Tree Level Order Traversal.md

100644100755
File mode changed.

leetcode/437. Path Sum III.md

100644100755
File mode changed.

leetcode/449. Serialize and Deserialize BST.md

100644100755
File mode changed.

leetcode/508. Most Frequent Subtree Sum.md

100644100755
File mode changed.

leetcode/543. Diameter of Binary Tree.md

100644100755
File mode changed.

leetcode/572. Subtree of Another Tree.md

100644100755
File mode changed.

leetcode/589. N-ary Tree Preorder Traversal.md

100644100755
File mode changed.

leetcode/590. N-ary Tree Postorder Traversal.md

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 668. Kth Smallest Number in Multiplication Table
2+
3+
### Question
4+
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?
5+
6+
Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.
7+
8+
```
9+
Example 1:
10+
Input: m = 3, n = 3, k = 5
11+
Output:
12+
Explanation:
13+
The Multiplication Table:
14+
1 2 3
15+
2 4 6
16+
3 6 9
17+
18+
The 5-th smallest number is 3 (1, 2, 2, 3, 3).
19+
Example 2:
20+
Input: m = 2, n = 3, k = 6
21+
Output:
22+
Explanation:
23+
The Multiplication Table:
24+
1 2 3
25+
2 4 6
26+
27+
The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).
28+
```
29+
30+
Note:
31+
1. The m and n will be in the range [1, 30000].
32+
2. The k will be in the range [1, m * n]
33+
34+
### Solution
35+
* Method 1: binary search
36+
```Java
37+
class Solution {
38+
public int findKthNumber(int m, int n, int k) {
39+
int low = 1, high = m * n;
40+
while(low <= high){
41+
int mid = low + (high - low) / 2;
42+
int count = getLowerNum(m, n, mid);
43+
if(count < k) low = mid + 1;
44+
else high = mid - 1;
45+
}
46+
return low;
47+
}
48+
private int getLowerNum(int m, int n, int target){
49+
int row = m - 1, col = 0;
50+
int res = 0;
51+
while(row >= 0 && col < n){
52+
if((row + 1) * (col + 1) > target) row--;
53+
else{
54+
res += row + 1;
55+
col++;
56+
}
57+
}
58+
return res;
59+
}
60+
}
61+
```

leetcode/669. Trim a Binary Search Tree.md

100644100755
File mode changed.

leetcode/687. Longest Univalue Path.md

100644100755
File mode changed.

leetcode/704. Binary Search.md

100644100755
File mode changed.

leetcode/778. Swim in Rising Water.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## 778. Swim in Rising Water
2+
3+
### Question
4+
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).
5+
6+
Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.
7+
8+
You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?
9+
10+
```
11+
Example 1:
12+
13+
Input: [[0,2],[1,3]]
14+
Output: 3
15+
Explanation:
16+
At time 0, you are in grid location (0, 0).
17+
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
18+
19+
You cannot reach point (1, 1) until time 3.
20+
When the depth of water is 3, we can swim anywhere inside the grid.
21+
Example 2:
22+
23+
Input: [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
24+
Output: 16
25+
Explanation:
26+
0 1 2 3 4
27+
24 23 22 21 5
28+
12 13 14 15 16
29+
11 17 18 19 20
30+
10 9 8 7 6
31+
32+
The final route is marked in bold.
33+
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
34+
```
35+
36+
Note:
37+
1. 2 <= N <= 50.
38+
2. grid[i][j] is a permutation of [0, ..., N*N - 1].
39+
40+
### Solutions:
41+
* Method 1: Dijkstra O(n^2NlgN)
42+
```Java
43+
class Solution {
44+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
45+
public int swimInWater(int[][] grid) {
46+
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
47+
@Override
48+
public int compare(int[] a, int[] b){
49+
return a[0] - b[0];
50+
}
51+
});
52+
int n = grid.length;
53+
pq.offer(new int[]{grid[0][0], 0});
54+
Set<Integer> seen = new HashSet<>();
55+
seen.add(0);
56+
while(!pq.isEmpty()){
57+
int[] node = pq.poll();
58+
int t = node[0];
59+
int row = node[1] / n;
60+
int col = node[1] % n;
61+
if(row == n - 1 && col == n - 1) return t;
62+
int tx = 0, ty = 0;
63+
for(int d = 0; d < 4; d++){
64+
tx = row + dir[d][0];
65+
ty = col + dir[d][1];
66+
if(seen.contains(tx * n + ty) || tx < 0 || tx >= n || ty < 0 || ty >= n) continue;
67+
seen.add(tx * n + ty);
68+
pq.offer(new int[]{Math.max(t, grid[tx][ty]), tx * n + ty});
69+
}
70+
}
71+
return -1;
72+
}
73+
}
74+
```

leetcode/814. Binary Tree Pruning.md

100644100755
File mode changed.

leetcode/852. Peak Index in a Mountain Array.md

100644100755
File mode changed.

leetcode/872. Leaf-Similar Trees.md

100644100755
File mode changed.

leetcode/924. Minimize Malware Spread.md

100644100755
File mode changed.

leetcode/959. Regions Cut By Slashes.md

100644100755
File mode changed.

leetcode/965. Univalued Binary Tree.md

100644100755
File mode changed.

leetcode/968. Binary Tree Cameras.md

100644100755
File mode changed.

leetcode/979. Distribute Coins in Binary Tree.md

100644100755
File mode changed.

leetcode/981. Time Based Key-Value Store.md

100644100755
File mode changed.

leetcode/987. Vertical Order Traversal of a Binary Tree.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)