2639. Find the Width of Columns of a Grid LeetCode Solution

In this guide, you will get 2639. Find the Width of Columns of a Grid LeetCode Solution with the best time and space complexity. The solution to Find the Width of Columns of a Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Width of Columns of a Grid solution in C++
  4. Find the Width of Columns of a Grid solution in Java
  5. Find the Width of Columns of a Grid solution in Python
  6. Additional Resources
2639. Find the Width of Columns of a Grid LeetCode Solution image

Problem Statement of Find the Width of Columns of a Grid

You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.

For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3.

Return an integer array ans of size n where ans[i] is the width of the ith column.
The length of an integer x with len digits is equal to len if x is non-negative, and len + 1 otherwise.

Example 1:

Input: grid = [[1],[22],[333]]
Output: [3]
Explanation: In the 0th column, 333 is of length 3.

Example 2:

Input: grid = [[-15,1,3],[15,7,12],[5,6,-2]]
Output: [3,1,2]
Explanation:
In the 0th column, only -15 is of length 3.
In the 1st column, all integers are of length 1.
In the 2nd column, both 12 and -2 are of length 2.

Constraints:

m == grid.length
n == grid[i].length
1 <= m, n <= 100
-109 <= grid[r][c] <= 109

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(n)

2639. Find the Width of Columns of a Grid LeetCode Solution in C++

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;
  }
};
/* code provided by PROGIEZ */

2639. Find the Width of Columns of a Grid LeetCode Solution in Java

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;
  }
}
// code provided by PROGIEZ

2639. Find the Width of Columns of a Grid LeetCode Solution in Python

class Solution:
  def findColumnWidth(self, grid: list[list[int]]) -> list[int]:
    return [max(map(len, map(str, col))) for col in zip(*grid)]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.