Skip to content

Commit 83c41df

Browse files
committedAug 28, 2017
refactor 378
1 parent 7d8be5c commit 83c41df

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Your ideas/fixes/algorithms are more than welcome!
249249
|381|[Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_381.java)| | | Hard|
250250
|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_380.java)| O(n) | O(1)| Medium| Design, HashMap
251251
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_379.java)| O(1)|O(n) | Medium|
252+
|378|[Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_378.java)| O(logm*n) | O(1)| Medium| Binary Search
252253
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_377.java)| O(?)|O(?) | Medium|
253254
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_376.java)| O(n)|O(1) | Medium| DP, Greedy
254255
|375|[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_375.java)| O(n^2)|O(n^2) | Medium| DP

‎src/main/java/com/fishercoder/solutions/_378.java

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.List;
6-
/**378. Kth Smallest Element in a Sorted Matrix
7-
*
8-
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.
6+
7+
/**
8+
* 378. Kth Smallest Element in a Sorted Matrix
9+
* Given a n x n matrix where each of the rows and columns are
10+
* sorted in ascending order, find the kth smallest element in the matrix.
911
1012
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
1113
@@ -20,48 +22,56 @@
2022
2123
return 13.
2224
23-
Note:
24-
You may assume k is always valid, 1 ≤ k ≤ n2.*/
25+
Note:
26+
You may assume k is always valid, 1 ≤ k ≤ n2.
27+
*/
28+
2529
public class _378 {
26-
//brute force made it AC'ed, extreme test case needed for OJ
27-
public int kthSmallest(int[][] matrix, int k) {
28-
List<Integer> list = new ArrayList<Integer>();
29-
int n = matrix.length;
30-
for (int i = 0; i < n; i++) {
31-
for (int j = 0; j < n; j++) {
32-
list.add(matrix[i][j]);
30+
public static class Solution1 {
31+
/**
32+
* brute force made it AC'ed, extreme test case needed for OJ
33+
*/
34+
public int kthSmallest(int[][] matrix, int k) {
35+
List<Integer> list = new ArrayList();
36+
int n = matrix.length;
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < n; j++) {
39+
list.add(matrix[i][j]);
40+
}
3341
}
42+
Collections.sort(list);
43+
return list.get(k - 1);
3444
}
35-
Collections.sort(list);
36-
return list.get(k - 1);
3745
}
3846

39-
//TODO: use heap and binary search to do it.
40-
41-
//Binary Search : The idea is to pick a mid number than compare it with the elements in each row, we start form
42-
// end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements
43-
// that less than mid and compare with k, then update the k.
44-
public int kthSmallestBS(int[][] matrix, int k) {
45-
int row = matrix.length - 1;
46-
int col = matrix[0].length - 1;
47-
int lo = matrix[0][0];
48-
int hi = matrix[row][col];
49-
while (lo < hi) {
50-
int mid = lo + (hi - lo) / 2;
51-
int count = 0;
52-
int j = col;
53-
for (int i = 0; i <= row; i++) {
54-
while (j >= 0 && matrix[i][j] > mid) {
55-
j--;
47+
public static class Solution2 {
48+
/**
49+
* Binary Search : The idea is to pick a mid number, then compare it with the elements in each row, we start form
50+
* end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements
51+
* that less than mid and compare with k, then update the k.
52+
*/
53+
public int kthSmallestBS(int[][] matrix, int k) {
54+
int row = matrix.length - 1;
55+
int col = matrix[0].length - 1;
56+
int lo = matrix[0][0];
57+
int hi = matrix[row][col];
58+
while (lo < hi) {
59+
int mid = lo + (hi - lo) / 2;
60+
int count = 0;
61+
int j = col;
62+
for (int i = 0; i <= row; i++) {
63+
while (j >= 0 && matrix[i][j] > mid) {
64+
j--;
65+
}
66+
count += (j + 1);
67+
}
68+
if (count < k) {
69+
lo = mid + 1;
70+
} else {
71+
hi = mid;
5672
}
57-
count += (j + 1);
58-
}
59-
if (count < k) {
60-
lo = mid + 1;
61-
} else {
62-
hi = mid;
6373
}
74+
return lo;
6475
}
65-
return lo;
6676
}
6777
}

0 commit comments

Comments
 (0)
Please sign in to comment.