{"id":10006,"date":"2023-04-29T08:22:36","date_gmt":"2023-04-29T15:22:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10006"},"modified":"2023-04-29T08:23:39","modified_gmt":"2023-04-29T15:23:39","slug":"leetcode-2639-find-the-width-of-columns-of-a-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2639-find-the-width-of-columns-of-a-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2639. Find the Width of Columns of a Grid"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;<code>m x n<\/code>&nbsp;integer matrix&nbsp;<code>grid<\/code>. The width of a column is the maximum&nbsp;<strong>length&nbsp;<\/strong>of its integers.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>grid = [[-10], [3], [12]]<\/code>, the width of the only column is&nbsp;<code>3<\/code>&nbsp;since&nbsp;<code>-10<\/code>&nbsp;is of length&nbsp;<code>3<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>an integer array<\/em>&nbsp;<code>ans<\/code>&nbsp;<em>of size<\/em>&nbsp;<code>n<\/code>&nbsp;<em>where<\/em>&nbsp;<code>ans[i]<\/code>&nbsp;<em>is the width of the<\/em>&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;<em>column<\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>length<\/strong>&nbsp;of an integer&nbsp;<code>x<\/code>&nbsp;with&nbsp;<code>len<\/code>&nbsp;digits is equal to&nbsp;<code>len<\/code>&nbsp;if&nbsp;<code>x<\/code>&nbsp;is non-negative, and&nbsp;<code>len + 1<\/code>&nbsp;otherwise.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1],[22],[333]]\n<strong>Output:<\/strong> [3]\n<strong>Explanation:<\/strong> In the 0<sup>th<\/sup> column, 333 is of length 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[-15,1,3],[15,7,12],[5,6,-2]]\n<strong>Output:<\/strong> [3,1,2]\n<strong>Explanation:<\/strong> \nIn the 0<sup>th<\/sup> column, only -15 is of length 3.\nIn the 1<sup>st<\/sup> column, all integers are of length 1. \nIn the 2<sup>nd<\/sup> column, both 12 and -2 are of length 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 100<\/code><\/li><li><code>-10<sup>9<\/sup> &lt;= grid[r][c] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Note: width of &#8216;0&#8217; is 1.<\/p>\n\n\n\n<p>Time complexity: O(m*n*log(x))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> findColumnWidth(vector<vector<int>>& grid) {\n    vector<int> ans;\n    for (int j = 0; j < grid[0].size(); ++j) {\n      int maxWidth = 1;\n      for (int i = 0; i < grid.size(); ++i) {\n        int x = grid[i][j];\n        int width = 0;        \n        if (x < 0) {\n          x = -x;\n          ++width;\n        }\n        while (x) {\n          x \/= 10;\n          ++width;\n        }        \n        maxWidth = max(maxWidth, width);\n      }\n      ans.push_back(maxWidth);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;m x n&nbsp;integer matrix&nbsp;grid. The width of a column is the maximum&nbsp;length&nbsp;of its integers. For example, if&nbsp;grid = [[-10], [3], [12]], the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,503,216,179,793],"class_list":["post-10006","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-length","tag-matrix","tag-simulation","tag-width","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10006"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10006\/revisions"}],"predecessor-version":[{"id":10009,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10006\/revisions\/10009"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}