2639. Find the Width of Columns of a Grid
Problem
2639. Find the Width of Columns of a Grid
Approach
My first thought was to use map: build an expression like [list(map(lambda x: len(str(x)), row)) for row in grid], but the problem requires finding the maximum value per column. Doing that manually would require O(n²) time complexity, so I used numpy to easily find the maximum of each column in one pass.
Code
import numpy as np
class Solution:
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
# let's convert the grid to a numpy array
np_grid = np.array(grid, dtype=str)
# calculate the length of each element in the grid
lengths = np.vectorize(len)(np_grid)
# find the maximum length of each column
max_lengths = lengths.max(axis=0)
return max_lengths.tolist()impl Solution {
pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
let col_n = grid[0].len();
let mut ans = vec![0; col_n];
for row in grid.iter() {
for (i, &num) in row.iter().enumerate() {
let length = num.to_string().len() as i32;
if length > ans[i] {
ans[i] = length;
}
}
}
ans
}
}贡献者
这篇文章有帮助吗?
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0