{"id":8630,"date":"2021-10-21T21:23:04","date_gmt":"2021-10-22T04:23:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8630"},"modified":"2021-10-21T21:23:05","modified_gmt":"2021-10-22T04:23:05","slug":"leetcode-2033-minimum-operations-to-make-a-uni-value-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2033-minimum-operations-to-make-a-uni-value-grid\/","title":{"rendered":"LeetCode 2033. Minimum Operations to Make a Uni-Value Grid"},"content":{"rendered":"\n<p>You are given a 2D integer&nbsp;<code>grid<\/code>&nbsp;of size&nbsp;<code>m x n<\/code>&nbsp;and an integer&nbsp;<code>x<\/code>. In one operation, you can&nbsp;<strong>add<\/strong>&nbsp;<code>x<\/code>&nbsp;to or&nbsp;<strong>subtract<\/strong>&nbsp;<code>x<\/code>&nbsp;from any element in the&nbsp;<code>grid<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>uni-value grid<\/strong>&nbsp;is a grid where all the elements of it are equal.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of operations to make the grid&nbsp;<strong>uni-value<\/strong><\/em>. If it is not possible, return&nbsp;<code>-1<\/code>.<\/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\/2021\/09\/21\/gridtxt.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[2,4],[6,8]], x = 2\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> We can make every element equal to 4 by doing the following: \n- Add x to 2 once.\n- Subtract x from 6 once.\n- Subtract x from 8 twice.\nA total of 4 operations were used.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/21\/gridtxt-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,5],[2,3]], x = 1\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> We can make every element equal to 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/21\/gridtxt-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,2],[3,4]], x = 2\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> It is impossible to make every element equal.\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;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= m * n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= x, grid[i][j] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Median<\/strong><\/h2>\n\n\n\n<p>To achieve minimum operations, the uni-value must be the median of the array.<\/p>\n\n\n\n<p>Time complexity: O(m*n)<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  int minOperations(vector<vector<int>>& grid, int x) {\n    const int mn = grid.size() * grid[0].size();    \n    vector<int> nums;\n    nums.reserve(mn);\n    for (const auto& row : grid)\n      nums.insert(end(nums), begin(row), end(row));\n    nth_element(begin(nums), begin(nums) + mn \/ 2, end(nums));\n    const int median = nums[mn \/ 2];\n    int ans = 0;\n    for (int v : nums) {\n      if (abs(v - median) % x) return -1;\n      ans += abs(v - median) \/ x;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D integer&nbsp;grid&nbsp;of size&nbsp;m x n&nbsp;and an integer&nbsp;x. In one operation, you can&nbsp;add&nbsp;x&nbsp;to or&nbsp;subtract&nbsp;x&nbsp;from any element in the&nbsp;grid. A&nbsp;uni-value grid&nbsp;is a grid&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,11,177],"class_list":["post-8630","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-median","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8630","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=8630"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8630\/revisions"}],"predecessor-version":[{"id":8631,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8630\/revisions\/8631"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}