Skip to content

Commit

Permalink
feat: add typescript solution to lc problem: No.2571 (#1894)
Browse files Browse the repository at this point in the history
No.2571.Minimum Operations to Reduce an Integer to 0
  • Loading branch information
yanglbme authored Oct 28, 2023
1 parent e24cd4d commit a27055d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ func minOperations(n int) (ans int) {
}
```

### **TypeScript**

```ts
function minOperations(n: number): number {
let [ans, cnt] = [0, 0];
for (; n; n >>= 1) {
if (n & 1) {
++cnt;
} else if (cnt) {
++ans;
cnt = cnt === 1 ? 0 : 1;
}
}
if (cnt === 1) {
++ans;
} else if (cnt > 1) {
ans += 2;
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ So the minimum number of operations is 3.

## Solutions

**Solution 1: Greedy + Bitwise Operation**

We convert the integer $n$ to binary, starting from the lowest bit:

- If the current bit is 1, we accumulate the current number of consecutive 1s;
- If the current bit is 0, we check whether the current number of consecutive 1s is greater than 0. If it is, we check whether the current number of consecutive 1s is 1. If it is, it means that we can eliminate 1 through one operation; if it is greater than 1, it means that we can reduce the number of consecutive 1s to 1 through one operation.

Finally, we also need to check whether the current number of consecutive 1s is 1. If it is, it means that we can eliminate 1 through one operation; if it is greater than 1, we can eliminate the consecutive 1s through two operations.

The time complexity is $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the given integer in the problem.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -139,6 +150,28 @@ func minOperations(n int) (ans int) {
}
```

### **TypeScript**

```ts
function minOperations(n: number): number {
let [ans, cnt] = [0, 0];
for (; n; n >>= 1) {
if (n & 1) {
++cnt;
} else if (cnt) {
++ans;
cnt = cnt === 1 ? 0 : 1;
}
}
if (cnt === 1) {
++ans;
} else if (cnt > 1) {
ans += 2;
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function minOperations(n: number): number {
let [ans, cnt] = [0, 0];
for (; n; n >>= 1) {
if (n & 1) {
++cnt;
} else if (cnt) {
++ans;
cnt = cnt === 1 ? 0 : 1;
}
}
if (cnt === 1) {
++ans;
} else if (cnt > 1) {
ans += 2;
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ Thus, there are four possible ways to group them:

## Solutions

**Solution 1: Sorting + Counting + Fast Power**

We can first sort the intervals in the range, merge the overlapping intervals, and count the number of non-overlapping intervals, denoted as $cnt$.

Each non-overlapping interval can be chosen to be put in the first group or the second group, so the number of plans is $2^{cnt}$. Note that $2^{cnt}$ may be very large, so we need to take modulo $10^9 + 7$. Here, we can use fast power to solve this problem.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of intervals.

Alternatively, we can also avoid using fast power. Once a new non-overlapping interval is found, we multiply the number of plans by 2 and take modulo $10^9 + 7$.

<!-- tabs:start -->

### **Python3**
Expand Down

0 comments on commit a27055d

Please sign in to comment.