{"id":1822,"date":"2018-02-20T18:22:22","date_gmt":"2018-02-21T02:22:22","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1822"},"modified":"2018-08-19T09:24:39","modified_gmt":"2018-08-19T16:24:39","slug":"leetcode-378-kth-smallest-element-in-a-sorted-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-378-kth-smallest-element-in-a-sorted-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 378. Kth Smallest Element in a Sorted Matrix"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u77e9\u9635\uff0c\u6bcf\u884c\u6bcf\u5217\u5404\u81ea\u6392\u5e8f\u3002\u627e\u51fa\u77e9\u9635\u4e2d\u7b2cK\u5c0f\u7684\u5143\u7d20\u3002<\/p>\n<p>Given a\u00a0<i>n<\/i>\u00a0x\u00a0<i>n<\/i>\u00a0matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.<\/p>\n<p>Note that it is the kth smallest element in the sorted order, not the kth distinct element.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre>matrix = [\r\n   [ 1,  5,  9],\r\n   [10, 11, 13],\r\n   [12, 13, 15]\r\n],\r\nk = 8,\r\n\r\nreturn 13.\r\n<\/pre>\n<p><b>Note:\u00a0<\/b><br \/>\nYou may assume k is always valid, 1 \u2264 k \u2264 n<sup>2<\/sup>.<\/p>\n<h1><strong>Solution 1: Binary Search<\/strong><\/h1>\n<p>Find the smallest x, such that there are k elements that are smaller or equal to x.<\/p>\n<p>Time complexity: O(nlogn*log(max &#8211; min))<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms (beats 97.12%)\r\nclass Solution {\r\npublic:\r\n  int kthSmallest(vector&lt;vector&lt;int&gt;&gt;&amp; matrix, int k) {\r\n    const int n = matrix.size();\r\n    long l = matrix[0][0];\r\n    long r = matrix[n - 1][n - 1] + 1;\r\n    while (l &lt; r) {\r\n      long m = l + (r - l) \/ 2;      \r\n      int total = 0;\r\n      int s = n;\r\n      for (const auto&amp; row : matrix)\r\n        total += (s = distance(begin(row), upper_bound(begin(row), begin(row) + s, m)));\r\n      if (total &gt;= k)\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u77e9\u9635\uff0c\u6bcf\u884c\u6bcf\u5217\u5404\u81ea\u6392\u5e8f\u3002\u627e\u51fa\u77e9\u9635\u4e2d\u7b2cK\u5c0f\u7684\u5143\u7d20\u3002 Given a\u00a0n\u00a0x\u00a0n\u00a0matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that&#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,383,177],"class_list":["post-1822","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-kth","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1822","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=1822"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":3617,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1822\/revisions\/3617"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}