Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit df1b485

Browse files
committed
Add solution #2639
1 parent 5df4e7b commit df1b485

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,7 @@
19601960
2634|[Filter Elements from Array](./solutions/2634-filter-elements-from-array.js)|Easy|
19611961
2635|[Apply Transform Over Each Element in Array](./solutions/2635-apply-transform-over-each-element-in-array.js)|Easy|
19621962
2637|[Promise Time Limit](./solutions/2637-promise-time-limit.js)|Medium|
1963+
2639|[Find the Width of Columns of a Grid](./solutions/2639-find-the-width-of-columns-of-a-grid.js)|Easy|
19631964
2648|[Generate Fibonacci Sequence](./solutions/2648-generate-fibonacci-sequence.js)|Easy|
19641965
2649|[Nested Array Generator](./solutions/2649-nested-array-generator.js)|Medium|
19651966
2650|[Design Cancellable Function](./solutions/2650-design-cancellable-function.js)|Hard|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2639. Find the Width of Columns of a Grid
3+
* https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 0-indexed m x n integer matrix grid. The width of a column is the
7+
* maximum length of its integers.
8+
* - For example, if grid = [[-10], [3], [12]], the width of the only column is 3
9+
* since -10 is of length 3.
10+
*
11+
* Return an integer array ans of size n where ans[i] is the width of the ith column.
12+
*
13+
* The length of an integer x with len digits is equal to len if x is non-negative, and
14+
* len + 1 otherwise.
15+
*/
16+
17+
/**
18+
* @param {number[][]} grid
19+
* @return {number[]}
20+
*/
21+
var findColumnWidth = function(grid) {
22+
const rows = grid.length;
23+
const cols = grid[0].length;
24+
const result = new Array(cols).fill(0);
25+
26+
for (let col = 0; col < cols; col++) {
27+
for (let row = 0; row < rows; row++) {
28+
const num = grid[row][col];
29+
result[col] = Math.max(result[col], String(num).length);
30+
}
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)