forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-the-width-of-columns-of-a-grid.cpp
42 lines (39 loc) · 1.08 KB
/
find-the-width-of-columns-of-a-grid.cpp
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
// Time: O(m * n)
// Space: O(1)
// array
class Solution {
public:
vector<int> findColumnWidth(vector<vector<int>>& grid) {
const auto& length = [](int x) {
int l = 1;
if (x < 0) {
x = -x;
++l;
}
for (; x >= 10; x /= 10, ++l);
return l;
};
vector<int> result(size(grid[0]), numeric_limits<int>::min());
for (int j = 0; j < size(grid[0]); ++j) {
for (int i = 0; i < size(grid); ++i) {
result[j] = max(result[j], length(grid[i][j]));
}
}
return result;
}
};
// Time: O(m * n)
// Space: O(logr)
// array
class Solution2 {
public:
vector<int> findColumnWidth(vector<vector<int>>& grid) {
vector<int> result(size(grid[0]), numeric_limits<int>::min());
for (int j = 0; j < size(grid[0]); ++j) {
for (int i = 0; i < size(grid); ++i) {
result[j] = max(result[j], static_cast<int>(size(to_string(grid[i][j]))));
}
}
return result;
}
};