Skip to content

Commit 6c933c5

Browse files
authored
feat: update solutions to lc problems: No.1780,1782,1787 (doocs#2426)
1 parent 633e937 commit 6c933c5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Mathematical Analysis
47+
48+
We find that if a number $n$ can be expressed as the sum of several "different" powers of three, then in the ternary representation of $n$, each digit can only be $0$ or $1$.
49+
50+
Therefore, we convert $n$ to ternary and then check whether each digit is $0$ or $1$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return `false`; otherwise, $n$ can be expressed as the sum of several powers of three, and we return `true`.
51+
52+
The time complexity is $O(\log_3 n)$, and the space complexity is $O(1)$.
4753

4854
<!-- tabs:start -->
4955

solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ The answers for each of the queries are as follows:
5454

5555
## Solutions
5656

57-
### Solution 1
57+
### Solution 1: Hash Table + Sorting + Binary Search
58+
59+
From the problem, we know that the number of edges connected to the point pair $(a, b)$ is equal to the "number of edges connected to $a$" plus the "number of edges connected to $b$", minus the number of edges connected to both $a$ and $b$.
60+
61+
Therefore, we can first use the array $cnt$ to count the number of edges connected to each point, and use the hash table $g$ to count the number of each point pair.
62+
63+
Then, for each query $q$, we can enumerate $a$. For each $a$, we can find the first $b$ that satisfies $cnt[a] + cnt[b] > q$ through binary search, add the number to the current query answer, and then subtract some duplicate edges.
64+
65+
The time complexity is $O(q \times (n \times \log n + m))$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of points and edges respectively, and $q$ is the number of queries.
5866

5967
<!-- tabs:start -->
6068

solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,33 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Dynamic Programming
48+
49+
Notice that after modifying the array `nums`, the XOR result of any interval of length $k$ is equal to $0$. Therefore, for any $i$, we have:
50+
51+
$$
52+
nums[i] \oplus nums[i+1] \oplus ... \oplus nums[i+k-1] = 0
53+
$$
54+
55+
and
56+
57+
$$
58+
nums[i+1] \oplus nums[i+2] \oplus ... \oplus nums[i+k] = 0
59+
$$
60+
61+
Combining the two equations and the properties of XOR operation, we can get $nums[i] \oplus nums[i+k] = 0$, which means $nums[i]=nums[i+k]$. We find that the elements in the modified array `nums` are cyclic with a period of $k$. The numbers congruent modulo $k$ can only take a fixed value, and the XOR result of the first $k$ numbers must be $0$.
62+
63+
First, we count each group $i$, the number of elements in each group is $size[i]$, and the number of elements with value $v$ in each group is $cnt[i][v]$.
64+
65+
Next, we can use dynamic programming to solve it. Let $f[i][j]$ represent the minimum number of modifications with the XOR sum of the first $i+1$ groups being $j$. Since the value of each group is only related to the value of the previous group, we can use a rolling array to optimize the space complexity.
66+
67+
Redefine $f[j]$ to represent the minimum number of modifications with the XOR sum being $j$ when processing to the current group.
68+
69+
When transitioning states, there are two choices: one is to modify all the numbers in the current group to the same value, then we can choose the one with the smallest previous cost, plus the number of elements $size[i]$ in this group, the cost is $\min{f[0..n]} + size[i]$; the second is to modify all the numbers in the current group to some value $j$ of the current group, enumerate $j$ and the element $v$ of the current group, then the previous cost is $f[j \oplus v]$, the cost is $f[j \oplus v] + size[i] - cnt[i][v]$. Take the minimum value.
70+
71+
The final answer is $f[0]$.
72+
73+
The time complexity is $O(2^{C}\times k + n)$. Where $n$ is the length of the array `nums`, and $C$ is the maximum number of bits in the binary representation of the elements in `nums`, in this problem $C=10$.
4874

4975
<!-- tabs:start -->
5076

0 commit comments

Comments
 (0)