Skip to content

Commit ef11f0e

Browse files
committed
Sync LeetCode submission Runtime - 139 ms (82.89%), Memory - 38.7 MB (97.37%)
1 parent 5981886 commit ef11f0e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>You are given a 2D integer <code>grid</code> of size <code>m x n</code> and an integer <code>x</code>. In one operation, you can <strong>add</strong> <code>x</code> to or <strong>subtract</strong> <code>x</code> from any element in the <code>grid</code>.</p>
2+
3+
<p>A <strong>uni-value grid</strong> is a grid where all the elements of it are equal.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> number of operations to make the grid <strong>uni-value</strong></em>. If it is not possible, return <code>-1</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png" style="width: 164px; height: 165px;" />
10+
<pre>
11+
<strong>Input:</strong> grid = [[2,4],[6,8]], x = 2
12+
<strong>Output:</strong> 4
13+
<strong>Explanation:</strong> We can make every element equal to 4 by doing the following:
14+
- Add x to 2 once.
15+
- Subtract x from 6 once.
16+
- Subtract x from 8 twice.
17+
A total of 4 operations were used.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png" style="width: 164px; height: 165px;" />
22+
<pre>
23+
<strong>Input:</strong> grid = [[1,5],[2,3]], x = 1
24+
<strong>Output:</strong> 5
25+
<strong>Explanation:</strong> We can make every element equal to 3.
26+
</pre>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png" style="width: 164px; height: 165px;" />
30+
<pre>
31+
<strong>Input:</strong> grid = [[1,2],[3,4]], x = 2
32+
<strong>Output:</strong> -1
33+
<strong>Explanation:</strong> It is impossible to make every element equal.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>m == grid.length</code></li>
41+
<li><code>n == grid[i].length</code></li>
42+
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
44+
<li><code>1 &lt;= x, grid[i][j] &lt;= 10<sup>4</sup></code></li>
45+
</ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Approach 1: Sorting and Median
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n * log mn)
5+
# Space: O(m * n)
6+
7+
8+
class Solution:
9+
def minOperations(self, grid: List[List[int]], x: int) -> int:
10+
nums_array = []
11+
result = 0
12+
13+
for row in grid:
14+
for num in row:
15+
nums_array.append(num)
16+
17+
nums_array.sort()
18+
19+
length = len(nums_array)
20+
final_common_number = nums_array[length // 2]
21+
22+
for number in nums_array:
23+
if number % x != final_common_number % x:
24+
return -1
25+
26+
result += abs(final_common_number - number) // x
27+
28+
return result
29+

0 commit comments

Comments
 (0)