Problem

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.

Examples

Example 1

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

Example 2

1
2
3
4
5
6
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] <= 10^9

Solution

Method 1 – Column-wise Maximum String Length

Intuition

The width of a column is determined by the maximum length of the string representation of any integer in that column. For each column, check all rows and find the maximum string length.

Approach

  1. For each column index, iterate through all rows.
  2. For each value, convert it to a string and compute its length (including the minus sign for negatives).
  3. Track the maximum length for each column.
  4. Return the list of maximum lengths for all columns.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    vector<int> findColumnWidth(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<int> ans(n, 0);
        for (int j = 0; j < n; ++j) {
            for (int i = 0; i < m; ++i) {
                int len = to_string(grid[i][j]).size();
                ans[j] = max(ans[j], len);
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func findColumnWidth(grid [][]int) []int {
    m, n := len(grid), len(grid[0])
    ans := make([]int, n)
    for j := 0; j < n; j++ {
        for i := 0; i < m; i++ {
            l := len(strconv.Itoa(grid[i][j]))
            if l > ans[j] {
                ans[j] = l
            }
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int[] findColumnWidth(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[] ans = new int[n];
        for (int j = 0; j < n; ++j) {
            for (int i = 0; i < m; ++i) {
                int len = Integer.toString(grid[i][j]).length();
                ans[j] = Math.max(ans[j], len);
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    fun findColumnWidth(grid: Array<IntArray>): IntArray {
        val m = grid.size
        val n = grid[0].size
        val ans = IntArray(n)
        for (j in 0 until n) {
            for (i in 0 until m) {
                val len = grid[i][j].toString().length
                ans[j] = maxOf(ans[j], len)
            }
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
class Solution:
    def findColumnWidth(self, grid: list[list[int]]) -> list[int]:
        m, n = len(grid), len(grid[0])
        ans = [0] * n
        for j in range(n):
            for i in range(m):
                ans[j] = max(ans[j], len(str(grid[i][j])))
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
impl Solution {
    pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
        let m = grid.len();
        let n = grid[0].len();
        let mut ans = vec![0; n];
        for j in 0..n {
            for i in 0..m {
                let len = grid[i][j].to_string().len() as i32;
                ans[j] = ans[j].max(len);
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    findColumnWidth(grid: number[][]): number[] {
        const m = grid.length, n = grid[0].length;
        const ans = Array(n).fill(0);
        for (let j = 0; j < n; ++j) {
            for (let i = 0; i < m; ++i) {
                ans[j] = Math.max(ans[j], grid[i][j].toString().length);
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(mn) — Each cell is checked once.
  • 🧺 Space complexity: O(n) — For the output array.