Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tags:
<pre>
<strong>输入:</strong>grid = [[2,4],[6,8]], x = 2
<strong>输出:</strong>4
<strong>解释:</strong>可以执行下述操作使所有元素都等于 4 :
<strong>解释:</strong>可以执行下述操作使所有元素都等于 4 :
- 2 加 x 一次。
- 6 减 x 一次。
- 8 减 x 两次。
Expand Down Expand Up @@ -194,6 +194,44 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function minOperations(grid: number[][], x: number): number {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}
```

#### JavaScript

```js
function minOperations(grid, x) {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> grid = [[2,4],[6,8]], x = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can make every element equal to 4 by doing the following:
<strong>Explanation:</strong> We can make every element equal to 4 by doing the following:
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
Expand Down Expand Up @@ -186,6 +186,44 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function minOperations(grid: number[][], x: number): number {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}
```

#### JavaScript

```js
function minOperations(grid, x) {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function minOperations(grid, x) {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function minOperations(grid: number[][], x: number): number {
const arr = grid.flat(2);
arr.sort((a, b) => a - b);
const median = arr[Math.floor(arr.length / 2)];

let res = 0;
for (const val of arr) {
const c = Math.abs(val - median) / x;
if (c !== (c | 0)) return -1;
res += c;
}

return res;
}