{"id":8029,"date":"2021-01-23T22:23:11","date_gmt":"2021-01-24T06:23:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8029"},"modified":"2021-01-23T22:27:04","modified_gmt":"2021-01-24T06:27:04","slug":"leetcode-1738-find-kth-largest-xor-coordinate-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1738-find-kth-largest-xor-coordinate-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1738. Find Kth Largest XOR Coordinate Value"},"content":{"rendered":"\n<p>You are given a 2D&nbsp;<code>matrix<\/code>&nbsp;of size&nbsp;<code>m x n<\/code>, consisting of non-negative integers. You are also given an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>value<\/strong>&nbsp;of coordinate&nbsp;<code>(a, b)<\/code>&nbsp;of the matrix is the XOR of all&nbsp;<code>matrix[i][j]<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt;= a &lt; m<\/code>&nbsp;and&nbsp;<code>0 &lt;= j &lt;= b &lt; n<\/code>&nbsp;<strong>(0-indexed)<\/strong>.<\/p>\n\n\n\n<p>Find the&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;largest value&nbsp;<strong>(1-indexed)<\/strong>&nbsp;of all the coordinates of&nbsp;<code>matrix<\/code>.<\/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> matrix = [[5,2],[1,6]], k = 1\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.<\/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> matrix = [[5,2],[1,6]], k = 2\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[5,2],[1,6]], k = 3\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[5,2],[1,6]], k = 4\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The value of coordinate (1,1) is 5 XOR 2 XOR 1 XOR 6 = 0, which is the 4th largest value.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == matrix.length<\/code><\/li><li><code>n == matrix[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 1000<\/code><\/li><li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= m * n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-304-range-sum-query-2d-immutable\/\" data-type=\"post\" data-id=\"348\">\u82b1\u82b1\u9171 LeetCode 304. Range Sum Query 2D \u2013 Immutable<\/a><\/p>\n\n\n\n<p>xor[i][j] = matrix[i][j] ^ xor[i &#8211; 1][j &#8211; 1] ^ xor[i &#8211; 1][j] ^ xor[i][j- 1]<\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn)<\/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  int kthLargestValue(vector<vector<int>>& matrix, int k) {    \n    const int m = matrix.size(), n = matrix[0].size();\n    vector<int> v;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        v.push_back(matrix[i][j] \n                      ^= (i ? matrix[i - 1][j] : 0) \n                       ^ (j ? matrix[i][j - 1] : 0) \n                       ^ (i * j ? matrix[i - 1][j - 1] : 0));\n    nth_element(begin(v), begin(v) + k - 1, end(v), greater<int>());    \n    return v[k - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D&nbsp;matrix&nbsp;of size&nbsp;m x n, consisting of non-negative integers. You are also given an integer&nbsp;k. The&nbsp;value&nbsp;of coordinate&nbsp;(a, b)&nbsp;of the matrix is 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":[46],"tags":[18,693,691,63],"class_list":["post-8029","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-k-th","tag-submatrix","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8029","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=8029"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8029\/revisions"}],"predecessor-version":[{"id":8033,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8029\/revisions\/8033"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}