Skip to content

Commit c90d23a

Browse files
committed
added 2 java solutions which works on all updated test cases also updated readme file
1 parent 266868e commit c90d23a

File tree

1 file changed

+14
-6
lines changed
  • solution/2500-2599/2501.Longest Square Streak in an Array

1 file changed

+14
-6
lines changed

solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ It can be shown that every subsequence of length 4 is not a square streak.
7070

7171
### Solution 1: Hash Table + Enumeration
7272

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.
7478

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}$.
7681

7782
<!-- tabs:start -->
7883

@@ -297,14 +302,17 @@ func longestSquareStreak(nums []int) int {
297302

298303
### Solution 2: Memoization Search
299304

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}$.
301308

302309
The calculation process of the function $\textit{dfs}(x)$ is as follows:
303310

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)$.
306313

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.
308316

309317
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
310318

0 commit comments

Comments
 (0)