Skip to content

Commit 35d9fd0

Browse files
committed
feat: add solutions to lc problem: No.1337
No.1337.The K Weakest Rows in a Matrix
1 parent 150cd4a commit 35d9fd0

File tree

4 files changed

+100
-38
lines changed

4 files changed

+100
-38
lines changed

solution/1300-1399/1337.The K Weakest Rows in a Matrix/README.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,9 @@ k = 2
8484
class Solution:
8585
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
8686
m, n = len(mat), len(mat[0])
87-
res = []
88-
for row in mat:
89-
left, right = 0, n
90-
while left < right:
91-
mid = (left + right) >> 1
92-
if row[mid] == 0:
93-
right = mid
94-
else:
95-
left = mid + 1
96-
res.append(left)
87+
ans = [n - bisect_right(row[::-1], 0) for row in mat]
9788
idx = list(range(m))
98-
idx.sort(key=lambda x: res[x])
89+
idx.sort(key=lambda i: ans[i])
9990
return idx[:k]
10091
```
10192

@@ -194,4 +185,37 @@ public:
194185
};
195186
```
196187
188+
### **Go**
189+
190+
```go
191+
func kWeakestRows(mat [][]int, k int) []int {
192+
m, n := len(mat), len(mat[0])
193+
res := make([]int, m)
194+
var idx []int
195+
for i, row := range mat {
196+
idx = append(idx, i)
197+
left, right := 0, n
198+
for left < right {
199+
mid := (left + right) >> 1
200+
if row[mid] == 0 {
201+
right = mid
202+
} else {
203+
left = mid + 1
204+
}
205+
}
206+
res[i] = left
207+
}
208+
sort.Slice(idx, func(i, j int) bool {
209+
return res[idx[i]] < res[idx[j]] || (res[idx[i]] == res[idx[j]] && idx[i] < idx[j])
210+
})
211+
return idx[:k]
212+
}
213+
```
214+
215+
### **...**
216+
217+
```
218+
219+
```
220+
197221
<!-- tabs:end -->

solution/1300-1399/1337.The K Weakest Rows in a Matrix/README_EN.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,9 @@ Binary search & sort.
7979
class Solution:
8080
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
8181
m, n = len(mat), len(mat[0])
82-
res = []
83-
for row in mat:
84-
left, right = 0, n
85-
while left < right:
86-
mid = (left + right) >> 1
87-
if row[mid] == 0:
88-
right = mid
89-
else:
90-
left = mid + 1
91-
res.append(left)
82+
ans = [n - bisect_right(row[::-1], 0) for row in mat]
9283
idx = list(range(m))
93-
idx.sort(key=lambda x: res[x])
84+
idx.sort(key=lambda i: ans[i])
9485
return idx[:k]
9586
```
9687

@@ -187,4 +178,38 @@ public:
187178
};
188179
```
189180
181+
### **Go**
182+
183+
```go
184+
func kWeakestRows(mat [][]int, k int) []int {
185+
m, n := len(mat), len(mat[0])
186+
res := make([]int, m)
187+
var idx []int
188+
for i, row := range mat {
189+
idx = append(idx, i)
190+
left, right := 0, n
191+
for left < right {
192+
mid := (left + right) >> 1
193+
if row[mid] == 0 {
194+
right = mid
195+
} else {
196+
left = mid + 1
197+
}
198+
}
199+
res[i] = left
200+
}
201+
sort.Slice(idx, func(i, j int) bool {
202+
return res[idx[i]] < res[idx[j]] || (res[idx[i]] == res[idx[j]] && idx[i] < idx[j])
203+
})
204+
return idx[:k]
205+
}
206+
```
207+
208+
### **...**
209+
210+
```
211+
212+
```
213+
214+
190215
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func kWeakestRows(mat [][]int, k int) []int {
2+
m, n := len(mat), len(mat[0])
3+
res := make([]int, m)
4+
var idx []int
5+
for i, row := range mat {
6+
idx = append(idx, i)
7+
left, right := 0, n
8+
for left < right {
9+
mid := (left + right) >> 1
10+
if row[mid] == 0 {
11+
right = mid
12+
} else {
13+
left = mid + 1
14+
}
15+
}
16+
res[i] = left
17+
}
18+
sort.Slice(idx, func(i, j int) bool {
19+
return res[idx[i]] < res[idx[j]] || (res[idx[i]] == res[idx[j]] && idx[i] < idx[j])
20+
})
21+
return idx[:k]
22+
}
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
class Solution:
2-
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
3-
m, n = len(mat), len(mat[0])
4-
res = []
5-
for row in mat:
6-
left, right = 0, n
7-
while left < right:
8-
mid = (left + right) >> 1
9-
if row[mid] == 0:
10-
right = mid
11-
else:
12-
left = mid + 1
13-
res.append(left)
14-
idx = list(range(m))
15-
idx.sort(key=lambda x: res[x])
16-
return idx[:k]
1+
class Solution:
2+
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
3+
m, n = len(mat), len(mat[0])
4+
ans = [n - bisect_right(row[::-1], 0) for row in mat]
5+
idx = list(range(m))
6+
idx.sort(key=lambda i: ans[i])
7+
return idx[:k]

0 commit comments

Comments
 (0)