Skip to content

2639. Find the Width of Columns of a Grid

  • Time: $O(mn)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  vector<int> findColumnWidth(vector<vector<int>>& grid) {
    vector<int> ans;

    for (int j = 0; j < grid[0].size(); ++j) {
      ans.push_back(0);
      for (int i = 0; i < grid.size(); ++i)
        ans[j] = max(ans[j], static_cast<int>(to_string(grid[i][j]).length()));
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int[] findColumnWidth(int[][] grid) {
    int[] ans = new int[grid[0].length];

    for (int j = 0; j < grid[0].length; ++j) {
      ans[j] = 0;
      for (int i = 0; i < grid.length; ++i)
        ans[j] = Math.max(ans[j], String.valueOf(grid[i][j]).length());
    }

    return ans;
  }
}
1
2
3
class Solution:
  def findColumnWidth(self, grid: list[list[int]]) -> list[int]:
    return [max(map(len, map(str, col))) for col in zip(*grid)]