Skip to content

Commit 64a8514

Browse files
authored
feat: add solutions to lc problem: No.1769 (#3934)
1 parent e8fc580 commit 64a8514

File tree

8 files changed

+167
-10
lines changed

8 files changed

+167
-10
lines changed

solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,62 @@ int* minOperations(char* boxes, int* returnSize) {
447447
448448
<!-- solution:end -->
449449
450+
<!-- solution:start -->
451+
452+
### Solution 3
453+
454+
<!-- tabs:start -->
455+
456+
#### TypeScript
457+
458+
```ts
459+
function minOperations(boxes: string): number[] {
460+
const n = boxes.length;
461+
const ans = Array(n).fill(0);
462+
const ones: number[] = [];
463+
464+
for (let i = 0; i < n; i++) {
465+
if (+boxes[i]) {
466+
ones.push(i);
467+
}
468+
}
469+
470+
for (let i = 0; i < n; i++) {
471+
for (const j of ones) {
472+
ans[i] += Math.abs(i - j);
473+
}
474+
}
475+
476+
return ans;
477+
}
478+
```
479+
480+
#### JavaScript
481+
482+
```js
483+
function minOperations(boxes) {
484+
const n = boxes.length;
485+
const ans = Array(n).fill(0);
486+
const ones = [];
487+
488+
for (let i = 0; i < n; i++) {
489+
if (+boxes[i]) {
490+
ones.push(i);
491+
}
492+
}
493+
494+
for (let i = 0; i < n; i++) {
495+
for (const j of ones) {
496+
ans[i] += Math.abs(i - j);
497+
}
498+
}
499+
500+
return ans;
501+
}
502+
```
503+
504+
<!-- tabs:end -->
505+
506+
<!-- solution:end -->
507+
450508
<!-- problem:end -->

solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md

+58
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,62 @@ int* minOperations(char* boxes, int* returnSize) {
439439
440440
<!-- solution:end -->
441441
442+
<!-- solution:start -->
443+
444+
### Solution 3
445+
446+
<!-- tabs:start -->
447+
448+
#### TypeScript
449+
450+
```ts
451+
function minOperations(boxes: string): number[] {
452+
const n = boxes.length;
453+
const ans = Array(n).fill(0);
454+
const ones: number[] = [];
455+
456+
for (let i = 0; i < n; i++) {
457+
if (+boxes[i]) {
458+
ones.push(i);
459+
}
460+
}
461+
462+
for (let i = 0; i < n; i++) {
463+
for (const j of ones) {
464+
ans[i] += Math.abs(i - j);
465+
}
466+
}
467+
468+
return ans;
469+
}
470+
```
471+
472+
#### JavaScript
473+
474+
```js
475+
function minOperations(boxes) {
476+
const n = boxes.length;
477+
const ans = Array(n).fill(0);
478+
const ones = [];
479+
480+
for (let i = 0; i < n; i++) {
481+
if (+boxes[i]) {
482+
ones.push(i);
483+
}
484+
}
485+
486+
for (let i = 0; i < n; i++) {
487+
for (const j of ones) {
488+
ans[i] += Math.abs(i - j);
489+
}
490+
}
491+
492+
return ans;
493+
}
494+
```
495+
496+
<!-- tabs:end -->
497+
498+
<!-- solution:end -->
499+
442500
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function minOperations(boxes) {
2+
const n = boxes.length;
3+
const ans = Array(n).fill(0);
4+
const ones = [];
5+
6+
for (let i = 0; i < n; i++) {
7+
if (+boxes[i]) {
8+
ones.push(i);
9+
}
10+
}
11+
12+
for (let i = 0; i < n; i++) {
13+
for (const j of ones) {
14+
ans[i] += Math.abs(i - j);
15+
}
16+
}
17+
18+
return ans;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function minOperations(boxes: string): number[] {
2+
const n = boxes.length;
3+
const ans = Array(n).fill(0);
4+
const ones: number[] = [];
5+
6+
for (let i = 0; i < n; i++) {
7+
if (+boxes[i]) {
8+
ones.push(i);
9+
}
10+
}
11+
12+
for (let i = 0; i < n; i++) {
13+
for (const j of ones) {
14+
ans[i] += Math.abs(i - j);
15+
}
16+
}
17+
18+
return ans;
19+
}

solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class Solution {
183183

184184
private int solve(int n, int m) {
185185
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
186-
pq.add(new int[]{n, n});
186+
pq.add(new int[] {n, n});
187187
Set<Integer> visited = new HashSet<>();
188188

189189
while (!pq.isEmpty()) {
@@ -207,7 +207,7 @@ class Solution {
207207
s[i] = (char) (s[i] + 1);
208208
int next = Integer.parseInt(new String(s));
209209
if (!sieve[next] && !visited.contains(next)) {
210-
pq.add(new int[]{sum + next, next});
210+
pq.add(new int[] {sum + next, next});
211211
}
212212
s[i] = c;
213213
}
@@ -216,7 +216,7 @@ class Solution {
216216
s[i] = (char) (s[i] - 1);
217217
int next = Integer.parseInt(new String(s));
218218
if (!sieve[next] && !visited.contains(next)) {
219-
pq.add(new int[]{sum + next, next});
219+
pq.add(new int[] {sum + next, next});
220220
}
221221
s[i] = c;
222222
}
@@ -286,6 +286,7 @@ private:
286286
}
287287
return -1;
288288
}
289+
289290
public:
290291
int minOperations(int n, int m) {
291292
runSieve();

solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class Solution {
180180

181181
private int solve(int n, int m) {
182182
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
183-
pq.add(new int[]{n, n});
183+
pq.add(new int[] {n, n});
184184
Set<Integer> visited = new HashSet<>();
185185

186186
while (!pq.isEmpty()) {
@@ -204,7 +204,7 @@ class Solution {
204204
s[i] = (char) (s[i] + 1);
205205
int next = Integer.parseInt(new String(s));
206206
if (!sieve[next] && !visited.contains(next)) {
207-
pq.add(new int[]{sum + next, next});
207+
pq.add(new int[] {sum + next, next});
208208
}
209209
s[i] = c;
210210
}
@@ -213,7 +213,7 @@ class Solution {
213213
s[i] = (char) (s[i] - 1);
214214
int next = Integer.parseInt(new String(s));
215215
if (!sieve[next] && !visited.contains(next)) {
216-
pq.add(new int[]{sum + next, next});
216+
pq.add(new int[] {sum + next, next});
217217
}
218218
s[i] = c;
219219
}
@@ -283,6 +283,7 @@ private:
283283
}
284284
return -1;
285285
}
286+
286287
public:
287288
int minOperations(int n, int m) {
288289
runSieve();

solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ class Solution {
4545
}
4646
return -1;
4747
}
48+
4849
public:
4950
int minOperations(int n, int m) {
5051
runSieve();
5152
if (sieve[n] || sieve[m]) return -1;
5253
return solve(n, m);
53-
}
54+
}
5455
};

solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private void runSieve() {
1717

1818
private int solve(int n, int m) {
1919
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
20-
pq.add(new int[]{n, n});
20+
pq.add(new int[] {n, n});
2121
Set<Integer> visited = new HashSet<>();
2222

2323
while (!pq.isEmpty()) {
@@ -41,7 +41,7 @@ private int solve(int n, int m) {
4141
s[i] = (char) (s[i] + 1);
4242
int next = Integer.parseInt(new String(s));
4343
if (!sieve[next] && !visited.contains(next)) {
44-
pq.add(new int[]{sum + next, next});
44+
pq.add(new int[] {sum + next, next});
4545
}
4646
s[i] = c;
4747
}
@@ -50,7 +50,7 @@ private int solve(int n, int m) {
5050
s[i] = (char) (s[i] - 1);
5151
int next = Integer.parseInt(new String(s));
5252
if (!sieve[next] && !visited.contains(next)) {
53-
pq.add(new int[]{sum + next, next});
53+
pq.add(new int[] {sum + next, next});
5454
}
5555
s[i] = c;
5656
}

0 commit comments

Comments
 (0)