carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode2639: Find the Width of Columns of a Grid #429

Open carloscn opened 1 year ago

carloscn commented 1 year ago

Description

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

carloscn commented 1 year ago

Analysis

pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32>
{
    let mut ret:Vec<i32> = vec![];
    if grid.len() < 1 {
        return ret;
    }

    let len = grid[0].len();
    for i in 0..len {
        let mut max_len:usize = usize::MIN;
        let mut col_len:usize;
        for j in 0..grid.len() {
            col_len = grid[j][i].to_string().len();
            max_len = max_len.max(col_len);
        }
        ret.push(max_len as i32);
    }

    return ret;
}
carloscn commented 1 year ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/1172625 https://github.com/carloscn/structstudy/commit/a0000b7d4fc0bd482519b4c546761542d7627452