Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3629f04

Browse files
committed
22
1 parent dc24418 commit 3629f04

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

5/KSmallest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public int[] kSmallestII(int[] array, int k) {
4040
if (array.length == 0 || k == 0) {
4141
return new int[0];
4242
}
43-
quickSelect(array, 0, array.length - 1, k - 1);
43+
quickSelect(array, 0, array.length - 1, k - 1); // ( [4,8,5,1], 0, 3, 2 )
4444
int[] result = Arrays.copyOf(array, k);
4545
Arrays.sort(result);
4646
return result;
4747
}
4848

4949
private void quickSelect(int[] array, int left, int right, int target) {
50-
int mid = partition(array, left, right);
50+
int mid = partition(array, left, right); // mid =
5151
if (mid == target) {
5252
return;
5353
} else if (target < mid) {
@@ -57,12 +57,12 @@ private void quickSelect(int[] array, int left, int right, int target) {
5757
}
5858
}
5959

60-
private int partition (int[] array, int left, int right) {
61-
int pivot = array[right];
62-
int start = left;
63-
int end = right - 1;
64-
while (start <= end) {
65-
if (array[start] < pivot) {
60+
private int partition (int[] array, int left, int right) { // [4,8,5,1], 0, 3
61+
int pivot = array[right]; // 1
62+
int start = left; // 0
63+
int end = right - 1; // 2
64+
while (start <= end) { // (0, 2)
65+
if (array[start] < pivot) { //
6666
start++;
6767
} else if (array[end] >= pivot) {
6868
end--;
@@ -86,3 +86,17 @@ public static void main(String[] args) {
8686
System.out.println(Arrays.toString(ks.kSmallestII(data, 3)));
8787
}
8888
}
89+
90+
91+
92+
/*
93+
a = [4,8,5,1] k = 3
94+
95+
96+
97+
98+
99+
100+
101+
102+
*/

KthSmallestNum.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* K-th Smallest Number In Sorted Matrix */
2+
public class KthSmallestNum {
3+
public int kthSmallest(int[][] matrix, int k) {
4+
int rows = matrix.length;
5+
int columns = matrix[0].length;
6+
// Best First Search, 每个值需要一个minheap
7+
PriorityQueue<Cell> minHeap = new PriorityQueue<Cell>(k, new Comparator<Cell>() {
8+
@Override
9+
public int compare(Cell c1, Cell c2) {
10+
if (c1.value == c2.value) {
11+
return 0;
12+
}
13+
return c1.value < c2.value ? -1 : 1;
14+
}
15+
});
16+
// 新生成的对象cell默认开始是true
17+
// 避免了多次生成同一个对象
18+
boolean[][] visited = new boolean[rows][columns];
19+
minHeap.offer(new Cell(0, 0, matrix[0][0]));
20+
21+
for (int i = 0; i < k - 1; i++) {
22+
Cell cur = minHeap.poll();
23+
if (cur.row + 1 < rows && !visited[cur.row + 1][cur.column]) {
24+
mminHeap.offer(new Cell(cur.row + 1, cur.column, matrix[cur.row + 1][cur.column]));
25+
26+
}
27+
28+
if (cur.column + 1 < columns && !visited[cur.row][cur.column + 1]) {
29+
minHeap.offer(new Cell(cur.row, cur.column + 1, matrix[cur.row][cur.column + 1]));
30+
31+
}
32+
33+
}
34+
return minHeap.peek().value;
35+
36+
}
37+
38+
static class Cell {
39+
int row;
40+
int column;
41+
int value;
42+
43+
Cell (int row, int column, int value) {
44+
this.row = row;
45+
this.column = column;
46+
this.value = value;
47+
}
48+
}
49+
}

KthSmallestNum.java~

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class KthSmallestNum

0 commit comments

Comments
 (0)