Skip to content
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
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Solution {

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

while (!pq.isEmpty()) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -286,6 +286,7 @@ private:
}
return -1;
}

public:
int minOperations(int n, int m) {
runSieve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Solution {

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

while (!pq.isEmpty()) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -283,6 +283,7 @@ private:
}
return -1;
}

public:
int minOperations(int n, int m) {
runSieve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private void runSieve() {

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

while (!pq.isEmpty()) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading