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/2500-2599/2501.Longest Square Streak in an Array/README_EN.md
+14-6Lines changed: 14 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,9 +70,14 @@ It can be shown that every subsequence of length 4 is not a square streak.
70
70
71
71
### Solution 1: Hash Table + Enumeration
72
72
73
-
We first use a hash table to record all elements in the array. Then, we enumerate each element in the array as the first element of the subsequence, square this element continuously, and check whether the squared result is in the hash table. If it is, we use the squared result as the next element and continue checking until the squared result is not in the hash table. At this point, we check whether the length of the subsequence is greater than $1$. If it is, we update the answer.
73
+
We first use a hash table to record all elements in the array. Then, we enumerate each element in the array as the first
74
+
element of the subsequence, square this element continuously, and check whether the squared result is in the hash table.
75
+
If it is, we use the squared result as the next element and continue checking until the squared result is not in the
76
+
hash table. At this point, we check whether the length of the subsequence is greater than $1$. If it is, we update the
77
+
answer.
74
78
75
-
The time complexity is $O(n \times \log \log M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$, and $M$ is the maximum value of the elements in the array $\textit{nums}$.
79
+
The time complexity is $O(n \times \log \log M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the
80
+
array $\textit{nums}$, and $M$ is the maximum value of the elements in the array $\textit{nums}$.
76
81
77
82
<!-- tabs:start -->
78
83
@@ -297,14 +302,17 @@ func longestSquareStreak(nums []int) int {
297
302
298
303
### Solution 2: Memoization Search
299
304
300
-
Similar to Solution 1, we first use a hash table to record all elements in the array. Then, we design a function $\textit{dfs}(x)$, which represents the length of the square wave starting with $x$. The answer is $\max(\textit{dfs}(x))$, where $x$ is an element in the array $\textit{nums}$.
305
+
Similar to Solution 1, we first use a hash table to record all elements in the array. Then, we design a
306
+
function $\textit{dfs}(x)$, which represents the length of the square wave starting with $x$. The answer
307
+
is $\max(\textit{dfs}(x))$, where $x$ is an element in the array $\textit{nums}$.
301
308
302
309
The calculation process of the function $\textit{dfs}(x)$ is as follows:
303
310
304
-
-If $x$ is not in the hash table, return $0$.
305
-
-Otherwise, return $1 + \textit{dfs}(x^2)$.
311
+
- If $x$ is not in the hash table, return $0$.
312
+
- Otherwise, return $1 + \textit{dfs}(x^2)$.
306
313
307
-
During the process, we can use memoization, i.e., use a hash table to record the value of the function $\textit{dfs}(x)$ to avoid redundant calculations.
314
+
During the process, we can use memoization, i.e., use a hash table to record the value of the function $\textit{dfs}(x)$
315
+
to avoid redundant calculations.
308
316
309
317
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
0 commit comments