-
Notifications
You must be signed in to change notification settings - Fork 115
/
1428-LeftmostColumnWithAtLeastAOne.cs
70 lines (58 loc) · 1.94 KB
/
1428-LeftmostColumnWithAtLeastAOne.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 27.1 MB
// Link: https://leetcode.com/submissions/detail/328251880/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
/**
* // This is BinaryMatrix's API interface.
* // You should not implement it, or speculate about its implementation
* class BinaryMatrix {
* public int Get(int x, int y) {}
* public IList<int> Dimensions() {}
* }
*/
public class _1428_LeftmostColumnWithAtLeastAOne
{
public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
{
var dimensions = binaryMatrix.Dimensions();
var row = 0;
var col = dimensions[1] - 1;
while (row < dimensions[0])
{
while (col >= 0 && binaryMatrix.Get(row, col) == 1) col--;
row++;
}
return col + 1 == dimensions[1] ? -1 : col + 1;
}
public class BinaryMatrix
{
private readonly int[][] numbers;
private int getCounts = 0;
private int n = 0;
private int m = 0;
public BinaryMatrix(int[][] num)
{
numbers = num;
getCounts = 0;
n = num.Length;
m = num[0].Length;
}
public int Get(int x, int y)
{
if (x < 0 || x >= n) throw new ArgumentException(nameof(x));
if (y < 0 || y >= m) throw new ArgumentException(nameof(y));
if (getCounts > 1000) throw new NotSupportedException("Wrong Anser");
return numbers[x][y];
}
public IList<int> Dimensions()
{
return new List<int>() { n, m };
}
}
}
}