Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1769
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored and yanglbme committed Jan 6, 2025
1 parent e8fc580 commit 9295858
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,62 @@ int* minOperations(char* boxes, int* returnSize) {
<!-- solution:end -->
<!-- solution:start -->
### Solution 3
<!-- tabs:start -->
#### TypeScript
```ts
function minOperations(boxes: string): number[] {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones: number[] = [];
for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}
for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}
return ans;
}
```

#### JavaScript

```js
function minOperations(boxes) {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones = [];

for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}

for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,62 @@ int* minOperations(char* boxes, int* returnSize) {
<!-- solution:end -->
<!-- solution:start -->
### Solution 3
<!-- tabs:start -->
#### TypeScript
```ts
function minOperations(boxes: string): number[] {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones: number[] = [];
for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}
for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}
return ans;
}
```

#### JavaScript

```js
function minOperations(boxes) {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones = [];

for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}

for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function minOperations(boxes) {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones = [];

for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}

for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function minOperations(boxes: string): number[] {
const n = boxes.length;
const ans = Array(n).fill(0);
const ones: number[] = [];

for (let i = 0; i < n; i++) {
if (+boxes[i]) {
ones.push(i);
}
}

for (let i = 0; i < n; i++) {
for (const j of ones) {
ans[i] += Math.abs(i - j);
}
}

return ans;
}

0 comments on commit 9295858

Please sign in to comment.