{"id":4848,"date":"2019-02-13T22:30:42","date_gmt":"2019-02-14T06:30:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4848"},"modified":"2019-02-13T22:31:19","modified_gmt":"2019-02-14T06:31:19","slug":"leetcode-74-search-a-2d-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-74-search-a-2d-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 74. Search a 2D Matrix"},"content":{"rendered":"\n<p>Write an efficient algorithm that searches for a value in an&nbsp;<em>m<\/em>&nbsp;x&nbsp;<em>n<\/em>&nbsp;matrix. This matrix has the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Integers in each row are sorted from left to right.<\/li><li>The first integer of each row is greater than the last integer of the previous row.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong>\nmatrix = [\n  [1,   3,  5,  7],\n  [10, 11, 16, 20],\n  [23, 30, 34, 50]\n]\ntarget = 3\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong>\nmatrix = [\n  [1,   3,  5,  7],\n  [10, 11, 16, 20],\n  [23, 30, 34, 50]\n]\ntarget = 13\n<strong>Output:<\/strong> false<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Treat the 2D array as a 1D array. matrix[index \/ cols][index % cols]<\/p>\n\n\n\n<p>Time complexity: O(log(m*n))<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++\">\nclass Solution {\npublic:\n  bool searchMatrix(vector<vector<int>>& matrix, int target) {\n    if (matrix.empty()) return false;\n    int l = 0;\n    int r = matrix.size() * matrix[0].size();\n    const int cols = matrix[0].size();\n    while (l < r) {\n      const int m = l + (r - l) \/ 2;\n      if (matrix[m \/ cols][m % cols] == target) {\n        return true;\n      } else if (matrix[m \/ cols][m % cols] > target) {\n        r = m;\n      } else {\n        l = m + 1;\n      }\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write an efficient algorithm that searches for a value in an&nbsp;m&nbsp;x&nbsp;n&nbsp;matrix. This matrix has the following properties: Integers in each row are sorted from left&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,216,177],"class_list":["post-4848","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-matrix","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4848","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=4848"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4848\/revisions"}],"predecessor-version":[{"id":4850,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4848\/revisions\/4850"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}