From a27055d019eb4e4540ed98c4a7886c8ea31b7677 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 28 Oct 2023 22:25:06 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.2571 (#1894) No.2571.Minimum Operations to Reduce an Integer to 0 --- .../README.md | 22 +++++++++++++ .../README_EN.md | 33 +++++++++++++++++++ .../Solution.ts | 17 ++++++++++ .../README_EN.md | 10 ++++++ 4 files changed, 82 insertions(+) create mode 100644 solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/Solution.ts diff --git a/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README.md b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README.md index be347cb0f05ae..c15e6ae87df9c 100644 --- a/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README.md +++ b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README_EN.md b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README_EN.md index 60cd900eff279..c1896242920d5 100644 --- a/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README_EN.md +++ b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/README_EN.md @@ -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. + ### **Python3** @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/Solution.ts b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/Solution.ts new file mode 100644 index 0000000000000..9e835de4f2a15 --- /dev/null +++ b/solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/Solution.ts @@ -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; +} diff --git a/solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README_EN.md b/solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README_EN.md index 3983cc53fe977..7e19254260589 100644 --- a/solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README_EN.md +++ b/solution/2500-2599/2580.Count Ways to Group Overlapping Ranges/README_EN.md @@ -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$. + ### **Python3**