{"id":6130,"date":"2020-01-26T08:58:39","date_gmt":"2020-01-26T16:58:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6130"},"modified":"2020-01-26T09:20:26","modified_gmt":"2020-01-26T17:20:26","slug":"leetcode-1329-sort-the-matrix-diagonally","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1329-sort-the-matrix-diagonally\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1329. Sort the Matrix Diagonally"},"content":{"rendered":"\n<p>Given a&nbsp;<code>m * n<\/code>&nbsp;matrix&nbsp;<code>mat<\/code>&nbsp;of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/21\/1482_example_1_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]\n<strong>Output:<\/strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m ==&nbsp;mat.length<\/code><\/li><li><code>n ==&nbsp;mat[i].length<\/code><\/li><li><code>1 &lt;= m, n&nbsp;&lt;= 100<\/code><\/li><li><code>1 &lt;= mat[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Collect each diagonal&#8217;s (keyed by i &#8211; j) elements into an array and sort it separately.<br>If  we offset the key by n, e.g. i &#8211; j + n, we can use an array instead of a hashtable.<\/p>\n\n\n\n<p>Time complexity: O(m*n + (m+n) * (m+n) * log(m + n))) =  (n^2*logn)<br>Space complexity: O(m*n)<\/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<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n    const int m = mat.size();\n    const int n = mat[0].size();\n    vector<deque<int>> qs(m + n);\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        qs[i - j + n].push_back(mat[i][j]);\n    for (auto&#038; q : qs)\n      sort(begin(q), end(q));\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j) {\n        mat[i][j] = qs[i - j + n].front();\n        qs[i - j + n].pop_front();\n      }\n    return mat;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m * n&nbsp;matrix&nbsp;mat&nbsp;of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array. Example 1: Input:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[535,82,216,177,15],"class_list":["post-6130","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-diagonal","tag-hashtable","tag-matrix","tag-medium","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6130","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=6130"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6130\/revisions"}],"predecessor-version":[{"id":6134,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6130\/revisions\/6134"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}