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

feat: add solutions to lc problem: No.2033 #4303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into doocs:main from rain84:feature/lc-2033
Mar 28, 2025
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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;
}
View file Open in desktop
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;
}

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