Skip to content

Commit d63bc24

Browse files
committed
chore: add daily leetcode post 2639. 查询网格图中每一列的宽度_translated
1 parent 6733c60 commit d63bc24

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: 2639. Query the width of each column in the grid diagram.md
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - answer
7+
abbrlink: 5a764983
8+
---
9+
10+
# topic:
11+
12+
[2639. Query the width of each column in the grid diagram.md](https://leetcode.cn/problems/find-the-width-of-columns-of-a-grid/description/?envType=daily-question&envId=2024-04-27)
13+
14+
# Thought:
15+
16+
The first thing I think is to usemapTo solve,`[list(map(lambda x: len(str(x)), row)) for row in grid]` Build such an expression,但是topic要求找到列的最大值,If it is done by yourself so manually, you need itO(n^2)Time complexity,SonumpyYou can easily find the iterative value of each column。
17+
18+
# Code:
19+
20+
```python
21+
import numpy as np
22+
23+
class Solution:
24+
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
25+
# let's convert the grid to a numpy array
26+
np_grid = np.array(grid, dtype=str)
27+
# calculate the length of each element in the grid
28+
lengths = np.vectorize(len)(np_grid)
29+
# find the maximum length of each column
30+
max_lengths = lengths.max(axis=0)
31+
return max_lengths.tolist()
32+
```
33+
```rust
34+
impl Solution {
35+
pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
36+
let col_n = grid[0].len();
37+
let mut ans = vec![0; col_n];
38+
39+
for row in grid.iter() {
40+
for (i, &num) in row.iter().enumerate() {
41+
let length = num.to_string().len() as i32;
42+
if length > ans[i] {
43+
ans[i] = length;
44+
}
45+
}
46+
}
47+
ans
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)