diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md index 78ededb534242..2bd99eddbb7e4 100644 --- a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md @@ -447,4 +447,62 @@ int* minOperations(char* boxes, int* returnSize) { + + +### Solution 3 + + + +#### 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; +} +``` + + + + + diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md index b907b1070e98f..40e753b7c3ef1 100644 --- a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md @@ -439,4 +439,62 @@ int* minOperations(char* boxes, int* returnSize) { + + +### Solution 3 + + + +#### 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; +} +``` + + + + + diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.js b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.js new file mode 100644 index 0000000000000..78a8903bb3290 --- /dev/null +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.js @@ -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; +} diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.ts b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.ts new file mode 100644 index 0000000000000..0123b2ca37b68 --- /dev/null +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.ts @@ -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; +} diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md index 9ab35abd4ccbd..265d404ff070e 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md @@ -183,7 +183,7 @@ class Solution { private int solve(int n, int m) { PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); - pq.add(new int[]{n, n}); + pq.add(new int[] {n, n}); Set visited = new HashSet<>(); while (!pq.isEmpty()) { @@ -207,7 +207,7 @@ class Solution { s[i] = (char) (s[i] + 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; } @@ -216,7 +216,7 @@ class Solution { s[i] = (char) (s[i] - 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; } @@ -286,6 +286,7 @@ private: } return -1; } + public: int minOperations(int n, int m) { runSieve(); diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md index 35cba5de2bebd..14c3fbf49fbde 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md @@ -180,7 +180,7 @@ class Solution { private int solve(int n, int m) { PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); - pq.add(new int[]{n, n}); + pq.add(new int[] {n, n}); Set visited = new HashSet<>(); while (!pq.isEmpty()) { @@ -204,7 +204,7 @@ class Solution { s[i] = (char) (s[i] + 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; } @@ -213,7 +213,7 @@ class Solution { s[i] = (char) (s[i] - 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; } @@ -283,6 +283,7 @@ private: } return -1; } + public: int minOperations(int n, int m) { runSieve(); diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp index ea2df83e39a04..31cef62861173 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp @@ -45,10 +45,11 @@ class Solution { } return -1; } + public: int minOperations(int n, int m) { runSieve(); if (sieve[n] || sieve[m]) return -1; return solve(n, m); - } + } }; diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java index a1fe469abf69d..285bdf80cf41d 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java @@ -17,7 +17,7 @@ private void runSieve() { private int solve(int n, int m) { PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); - pq.add(new int[]{n, n}); + pq.add(new int[] {n, n}); Set visited = new HashSet<>(); while (!pq.isEmpty()) { @@ -41,7 +41,7 @@ private int solve(int n, int m) { s[i] = (char) (s[i] + 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; } @@ -50,7 +50,7 @@ private int solve(int n, int m) { s[i] = (char) (s[i] - 1); int next = Integer.parseInt(new String(s)); if (!sieve[next] && !visited.contains(next)) { - pq.add(new int[]{sum + next, next}); + pq.add(new int[] {sum + next, next}); } s[i] = c; }