Skip to content

Commit a27055d

Browse files
authored
feat: add typescript solution to lc problem: No.2571 (#1894)
No.2571.Minimum Operations to Reduce an Integer to 0
1 parent e24cd4d commit a27055d

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ func minOperations(n int) (ans int) {
160160
}
161161
```
162162

163+
### **TypeScript**
164+
165+
```ts
166+
function minOperations(n: number): number {
167+
let [ans, cnt] = [0, 0];
168+
for (; n; n >>= 1) {
169+
if (n & 1) {
170+
++cnt;
171+
} else if (cnt) {
172+
++ans;
173+
cnt = cnt === 1 ? 0 : 1;
174+
}
175+
}
176+
if (cnt === 1) {
177+
++ans;
178+
} else if (cnt > 1) {
179+
ans += 2;
180+
}
181+
return ans;
182+
}
183+
```
184+
163185
### **...**
164186

165187
```

solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ So the minimum number of operations is 3.
4848

4949
## Solutions
5050

51+
**Solution 1: Greedy + Bitwise Operation**
52+
53+
We convert the integer $n$ to binary, starting from the lowest bit:
54+
55+
- If the current bit is 1, we accumulate the current number of consecutive 1s;
56+
- 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.
57+
58+
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.
59+
60+
The time complexity is $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the given integer in the problem.
61+
5162
<!-- tabs:start -->
5263

5364
### **Python3**
@@ -139,6 +150,28 @@ func minOperations(n int) (ans int) {
139150
}
140151
```
141152

153+
### **TypeScript**
154+
155+
```ts
156+
function minOperations(n: number): number {
157+
let [ans, cnt] = [0, 0];
158+
for (; n; n >>= 1) {
159+
if (n & 1) {
160+
++cnt;
161+
} else if (cnt) {
162+
++ans;
163+
cnt = cnt === 1 ? 0 : 1;
164+
}
165+
}
166+
if (cnt === 1) {
167+
++ans;
168+
} else if (cnt > 1) {
169+
ans += 2;
170+
}
171+
return ans;
172+
}
173+
```
174+
142175
### **...**
143176

144177
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function minOperations(n: number): number {
2+
let [ans, cnt] = [0, 0];
3+
for (; n; n >>= 1) {
4+
if (n & 1) {
5+
++cnt;
6+
} else if (cnt) {
7+
++ans;
8+
cnt = cnt === 1 ? 0 : 1;
9+
}
10+
}
11+
if (cnt === 1) {
12+
++ans;
13+
} else if (cnt > 1) {
14+
ans += 2;
15+
}
16+
return ans;
17+
}

solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ Thus, there are four possible ways to group them:
6060

6161
## Solutions
6262

63+
**Solution 1: Sorting + Counting + Fast Power**
64+
65+
We can first sort the intervals in the range, merge the overlapping intervals, and count the number of non-overlapping intervals, denoted as $cnt$.
66+
67+
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.
68+
69+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of intervals.
70+
71+
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$.
72+
6373
<!-- tabs:start -->
6474

6575
### **Python3**

0 commit comments

Comments
 (0)