You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0600-0699/0633.Sum of Square Numbers/README_EN.md
+30-22
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,11 @@
33
33
34
34
## Solutions
35
35
36
-
### Solution 1
36
+
### Solution 1: Mathematics + Two Pointers
37
+
38
+
We can use the two-pointer method to solve this problem. Define two pointers $a$ and $b$, pointing to $0$ and $\sqrt{c}$ respectively. In each step, we calculate the value of $s = a^2 + b^2$, and then compare the size of $s$ and $c$. If $s = c$, we have found two integers $a$ and $b$ such that $a^2 + b^2 = c$. If $s < c$, we increase the value of $a$ by $1$. If $s > c$, we decrease the value of $b$ by $1$. We continue this process until we find the answer, or the value of $a$ is greater than the value of $b$, and return `false`.
39
+
40
+
The time complexity is $O(\sqrt{c})$, where $c$ is the given non-negative integer. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0600-0699/0634.Find the Derangement of An Array/README_EN.md
+21-2
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,24 @@
35
35
36
36
## Solutions
37
37
38
-
### Solution 1
38
+
### Solution 1: Dynamic Programming
39
+
40
+
We define $f[i]$ as the number of derangement of an array of length $i$. Initially, $f[0] = 1$, $f[1] = 0$. The answer is $f[n]$.
41
+
42
+
For an array of length $i$, we consider where to place the number $1$. Suppose it is placed in the $j$-th position, where there are $i-1$ choices. Then, the number $j$ has two choices:
43
+
44
+
- Placed in the first position, then the remaining $i - 2$ positions have $f[i - 2]$ derangements, so there are a total of $(i - 1) \times f[i - 2]$ derangements;
45
+
- Not placed in the first position, which is equivalent to the derangement of an array of length $i - 1$, so there are a total of $(i - 1) \times f[i - 1]$ derangements.
46
+
47
+
In summary, we have the following state transition equation:
48
+
49
+
$$
50
+
f[i] = (i - 1) \times (f[i - 1] + f[i - 2])
51
+
$$
52
+
53
+
The final answer is $f[n]$. Note the modulo operation in the answer.
54
+
55
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
39
56
40
57
<!-- tabs:start -->
41
58
@@ -93,7 +110,9 @@ func findDerangement(n int) int {
We notice that the state transition equation only relates to $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $a$ and $b$ to represent $f[i - 1]$ and $f[i - 2]$ respectively, thereby reducing the space complexity to $O(1)$.
Store the `id` and `timestamp` of the logs as tuples in an array. Then in the `retrieve()` method, truncate the corresponding parts of `start` and `end` based on `granularity`, and traverse the array, adding the `id` that meets the conditions to the result array.
62
+
63
+
In terms of time complexity, the time complexity of the `put()` method is $O(1)$, and the time complexity of the `retrieve()` method is $O(n)$, where $n$ is the length of the array.
0 commit comments