-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1337-TheKWeakestRowsInAMatrix.cs
38 lines (34 loc) · 1.12 KB
/
1337-TheKWeakestRowsInAMatrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 248ms
// Memory Usage: 32.9 MB
// Link: https://leetcode.com/submissions/detail/330131749/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _1337_TheKWeakestRowsInAMatrix
{
public int[] KWeakestRows(int[][] mat, int k)
{
int m = mat.Length;
int n = mat[0].Length;
int[] results = new int[k];
int count = 0;
for (int col = 0; col < n && count < k; col++)
for (int row = 0; row < m && count < k; row++)
{
if (mat[row][col] == 0 && (col == 0 || mat[row][col - 1] == 1))
{
results[count] = row;
count++;
}
}
for (int row = 0; count < k; row++)
if (mat[row][n - 1] == 1)
{
results[count] = row;
count++;
}
return results;
}
}
}