File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 896
896
2016|[ Maximum Difference Between Increasing Elements] ( ./solutions/2016-maximum-difference-between-increasing-elements.js ) |Easy|
897
897
2017|[ Grid Game] ( ./solutions/2017-grid-game.js ) |Medium|
898
898
2027|[ Minimum Moves to Convert String] ( ./solutions/2027-minimum-moves-to-convert-string.js ) |Easy|
899
+ 2033|[ Minimum Operations to Make a Uni-Value Grid] ( ./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js ) |Medium|
899
900
2037|[ Minimum Number of Moves to Seat Everyone] ( ./solutions/2037-minimum-number-of-moves-to-seat-everyone.js ) |Easy|
900
901
2047|[ Number of Valid Words in a Sentence] ( ./solutions/2047-number-of-valid-words-in-a-sentence.js ) |Easy|
901
902
2053|[ Kth Distinct String in an Array] ( ./solutions/2053-kth-distinct-string-in-an-array.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2033. Minimum Operations to Make a Uni-Value Grid
3
+ * https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a 2D integer grid of size m x n and an integer x. In one operation, you can add x
7
+ * to or subtract x from any element in the grid.
8
+ *
9
+ * A uni-value grid is a grid where all the elements of it are equal.
10
+ *
11
+ * Return the minimum number of operations to make the grid uni-value. If it is not possible,
12
+ * return -1.
13
+ */
14
+
15
+ /**
16
+ * @param {number[][] } grid
17
+ * @param {number } x
18
+ * @return {number }
19
+ */
20
+ var minOperations = function ( grid , x ) {
21
+ if ( values . length === 1 ) return 0 ;
22
+ const values = grid . flat ( ) ;
23
+ values . sort ( ( a , b ) => a - b ) ;
24
+ const median = values [ Math . floor ( values . length / 2 ) ] ;
25
+ let operations = 0 ;
26
+
27
+ for ( const value of values ) {
28
+ const diff = Math . abs ( value - median ) ;
29
+ if ( diff % x !== 0 ) return - 1 ;
30
+ operations += diff / x ;
31
+ }
32
+
33
+ return operations ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments