From 9295858643a67bf5425988b2bd9761fcf4aee728 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 6 Jan 2025 22:06:56 +0300 Subject: [PATCH 1/5] feat: add solutions to lc problem: No.1769 --- .../README.md | 58 +++++++++++++++++++ .../README_EN.md | 58 +++++++++++++++++++ .../Solution3.js | 19 ++++++ .../Solution3.ts | 19 ++++++ 4 files changed, 154 insertions(+) create mode 100644 solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.js create mode 100644 solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/Solution3.ts 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; +} From 2834b417a0ea96a59aee6be9ec2aef58f02d0631 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 7 Jan 2025 07:50:10 +0800 Subject: [PATCH 2/5] Fix spacing in README.md code examples --- .../README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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(); From c9fe7c31576ff9ad542bb2ea84576b8b7acb3ad0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 7 Jan 2025 07:50:37 +0800 Subject: [PATCH 3/5] Format code in README_EN.md file --- .../README_EN.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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(); From a0df23c992e489ba2b1d7ca0917b2320ca71f7a3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 7 Jan 2025 07:50:59 +0800 Subject: [PATCH 4/5] Format code for consistency in Solution.java --- .../Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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; } From 44521e77a5fa8b2491b12529c161eeab21c5aa08 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 7 Jan 2025 07:51:08 +0800 Subject: [PATCH 5/5] Add missing closing brace in Solution.cpp --- .../Solution.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); - } + } };