Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4610f29

Browse files
authored
feat: add solutions to lc problem: No.2033 (doocs#4303)
1 parent 1be7636 commit 4610f29

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

‎solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README.md‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tags:
3636
<pre>
3737
<strong>输入:</strong>grid = [[2,4],[6,8]], x = 2
3838
<strong>输出:</strong>4
39-
<strong>解释:</strong>可以执行下述操作使所有元素都等于 4 :
39+
<strong>解释:</strong>可以执行下述操作使所有元素都等于 4 :
4040
- 2 加 x 一次。
4141
- 6 减 x 一次。
4242
- 8 减 x 两次。
@@ -194,6 +194,44 @@ func abs(x int) int {
194194
}
195195
```
196196

197+
#### TypeScript
198+
199+
```ts
200+
function minOperations(grid: number[][], x: number): number {
201+
const arr = grid.flat(2);
202+
arr.sort((a, b) => a - b);
203+
const median = arr[Math.floor(arr.length / 2)];
204+
205+
let res = 0;
206+
for (const val of arr) {
207+
const c = Math.abs(val - median) / x;
208+
if (c !== (c | 0)) return -1;
209+
res += c;
210+
}
211+
212+
return res;
213+
}
214+
```
215+
216+
#### JavaScript
217+
218+
```js
219+
function minOperations(grid, x) {
220+
const arr = grid.flat(2);
221+
arr.sort((a, b) => a - b);
222+
const median = arr[Math.floor(arr.length / 2)];
223+
224+
let res = 0;
225+
for (const val of arr) {
226+
const c = Math.abs(val - median) / x;
227+
if (c !== (c | 0)) return -1;
228+
res += c;
229+
}
230+
231+
return res;
232+
}
233+
```
234+
197235
<!-- tabs:end -->
198236

199237
<!-- solution:end -->

‎solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> grid = [[2,4],[6,8]], x = 2
3535
<strong>Output:</strong> 4
36-
<strong>Explanation:</strong> We can make every element equal to 4 by doing the following:
36+
<strong>Explanation:</strong> We can make every element equal to 4 by doing the following:
3737
- Add x to 2 once.
3838
- Subtract x from 6 once.
3939
- Subtract x from 8 twice.
@@ -186,6 +186,44 @@ func abs(x int) int {
186186
}
187187
```
188188

189+
#### TypeScript
190+
191+
```ts
192+
function minOperations(grid: number[][], x: number): number {
193+
const arr = grid.flat(2);
194+
arr.sort((a, b) => a - b);
195+
const median = arr[Math.floor(arr.length / 2)];
196+
197+
let res = 0;
198+
for (const val of arr) {
199+
const c = Math.abs(val - median) / x;
200+
if (c !== (c | 0)) return -1;
201+
res += c;
202+
}
203+
204+
return res;
205+
}
206+
```
207+
208+
#### JavaScript
209+
210+
```js
211+
function minOperations(grid, x) {
212+
const arr = grid.flat(2);
213+
arr.sort((a, b) => a - b);
214+
const median = arr[Math.floor(arr.length / 2)];
215+
216+
let res = 0;
217+
for (const val of arr) {
218+
const c = Math.abs(val - median) / x;
219+
if (c !== (c | 0)) return -1;
220+
res += c;
221+
}
222+
223+
return res;
224+
}
225+
```
226+
189227
<!-- tabs:end -->
190228

191229
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minOperations(grid, x) {
2+
const arr = grid.flat(2);
3+
arr.sort((a, b) => a - b);
4+
const median = arr[Math.floor(arr.length / 2)];
5+
6+
let res = 0;
7+
for (const val of arr) {
8+
const c = Math.abs(val - median) / x;
9+
if (c !== (c | 0)) return -1;
10+
res += c;
11+
}
12+
13+
return res;
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minOperations(grid: number[][], x: number): number {
2+
const arr = grid.flat(2);
3+
arr.sort((a, b) => a - b);
4+
const median = arr[Math.floor(arr.length / 2)];
5+
6+
let res = 0;
7+
for (const val of arr) {
8+
const c = Math.abs(val - median) / x;
9+
if (c !== (c | 0)) return -1;
10+
res += c;
11+
}
12+
13+
return res;
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /