GeezerVison is showing github.com — 1337_The_K_Weakest_Rows_in_a_Matrix · zhimintu/leetcode@85132d6
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -218,6 +218,7 @@ Also, there are open source implementations for basic data structs and algorithm |
218 | 218 | | 973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/973_K_Closest_Points_to_Origin.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/973_K_Closest_Points_to_Origin.java) | 1. Sort and get 0-K, O(nlogn) and O(1)<br>2. Min Heap, O(nlogk) and O(k) | |
219 | 219 | | 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) | |
220 | 220 | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) | |
| 221 | +| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) | |
221 | 222 |
|
222 | 223 | | # | To Understand | |
223 | 224 | |---| ----- | |
|
Collapse file
Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution { |
| 2 | + public int[] kWeakestRows(int[][] mat, int k) { |
| 3 | + List<Integer> res = new ArrayList<>(); |
| 4 | + int col = 0; |
| 5 | + boolean flag = true; |
| 6 | + while (col < mat[0].length && flag) { |
| 7 | + for (int i = 0; i < mat.length; i++) { |
| 8 | + if (res.contains(i)) continue; |
| 9 | + // Add first row with 0 into res |
| 10 | + if (mat[i][col] == 0) res.add(i); |
| 11 | + if (res.size() == k) { |
| 12 | + flag = false; |
| 13 | + break; |
| 14 | + } |
| 15 | + } |
| 16 | + col += 1; |
| 17 | + } |
| 18 | + if (flag) { |
| 19 | + // if res less than k |
| 20 | + for (int i = 0; i < mat.length; i++) { |
| 21 | + if (res.contains(i)) continue; |
| 22 | + res.add(i); |
| 23 | + if (res.size() == k) break; |
| 24 | + } |
| 25 | + } |
| 26 | + int[] ret = new int[k]; |
| 27 | + for (int i = 0; i < k; i++) ret[i] = res.get(i); |
| 28 | + return ret; |
| 29 | + } |
| 30 | +} |
Collapse file
Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution(object): |
| 2 | + def kWeakestRows(self, mat, k): |
| 3 | + """ |
| 4 | + :type mat: List[List[int]] |
| 5 | + :type k: int |
| 6 | + :rtype: List[int] |
| 7 | + """ |
| 8 | + res = [] |
| 9 | + num_row = len(mat) |
| 10 | + num_col = len(mat[0]) |
| 11 | + col = 0 |
| 12 | + flag = 1 |
| 13 | + while col < num_col and flag: |
| 14 | + for i in range(num_row): |
| 15 | + if i in res: |
| 16 | + continue |
| 17 | + # Add first row with 0 into res |
| 18 | + if mat[i][col] == 0: |
| 19 | + res.append(i) |
| 20 | + if len(res) == k: |
| 21 | + flag = 0 |
| 22 | + break |
| 23 | + col += 1 |
| 24 | + if len(res) == k: |
| 25 | + return res |
| 26 | + # if res less than k |
| 27 | + for i in range(num_row): |
| 28 | + if i in res: |
| 29 | + continue |
| 30 | + res.append(i) |
| 31 | + if len(res) == k: |
| 32 | + break |
| 33 | + return res |