@@ -118,6 +118,131 @@ class Solution {
118118}
119119```
120120
121+ #### Java2
122+
123+ ``` java
124+ class Solution {
125+ private Map<Integer , Integer > f = new HashMap<> ();
126+ private Set<Integer > s = new HashSet<> ();
127+
128+ public int longestSquareStreak (int [] nums ) {
129+ for (int v : nums) {
130+ s. add(v);
131+ }
132+ int ans = 0 ;
133+ for (int v : nums) {
134+ ans = Math . max(ans, dfs(v));
135+ }
136+ return ans < 2 ? - 1 : ans;
137+ }
138+
139+ private int dfs (int x ) {
140+ if (! s. contains(x)) {
141+ return 0 ;
142+ }
143+ if (f. containsKey(x)) {
144+ return f. get(x);
145+ }
146+ int ans = 1 + dfs(x * x);
147+ f. put(x, ans);
148+ return ans;
149+ }
150+ }
151+ ```
152+
153+ #### Java3
154+ ``` java3
155+ class Solution {
156+ private Map<Integer, Integer> memo = new HashMap<>(); // Memoization map
157+ private Set<Integer> numSet = new HashSet<>(); // Set of numbers
158+
159+ public int longestSquareStreak(int[] nums) {
160+ // Early exit if the array length is less than 2
161+ if (nums.length < 2)
162+ return -1;
163+
164+ // Add numbers to the set for quick lookup
165+ for (int n : nums) {
166+ numSet.add(n);
167+ }
168+
169+ int maxStreak = 0; // Track the maximum streak
170+ // Calculate the longest square streak for each number
171+ for (int n : nums) {
172+ maxStreak = Math.max(maxStreak, dfs(n));
173+ }
174+
175+ return maxStreak < 2 ? -1 : maxStreak; // Return result
176+ }
177+
178+ private int dfs(int x) {
179+ // Base case: if x is not in the set, return 0
180+ if (!numSet.contains(x)) {
181+ return 0;
182+ }
183+
184+ // Return cached result if already computed
185+ if (memo.containsKey(x)) {
186+ return memo.get(x);
187+ }
188+
189+ // Calculate the next number in the square streak
190+ long next = (long) x * x; // Use long to avoid overflow
191+ // If the next number exceeds the maximum allowed value, return 0
192+ if (next > 100000) {
193+ memo.put(x, 1); // Only count this number itself
194+ return 1;
195+ }
196+
197+ // Recursively calculate the streak
198+ int streak = 1 + dfs((int) next); // Include the current number in the streak
199+ memo.put(x, streak); // Cache the result
200+ return streak;
201+ }
202+ }
203+ ```
204+
205+ #### Java4
206+ ``` java4
207+ public class Solution {
208+ public int longestSquareStreak(int[] nums) {
209+ int maxStreak = -1; // Track the maximum streak
210+ final int MAX_NUM = (int) Math.pow(10, 5); // Maximum value for nums
211+ boolean[] exists = new boolean[MAX_NUM + 1]; // Track existence of numbers
212+ boolean[] visited = new boolean[MAX_NUM + 1]; // Track visited numbers
213+
214+ // Mark existing numbers
215+ for (int num : nums) {
216+ exists[num] = true;
217+ }
218+
219+ // Check for square streaks
220+ for (int i = 2; i * i <= MAX_NUM; i++) {
221+ if (!exists[i] || visited[i]) {
222+ continue; // Skip if number doesn't exist or is already visited
223+ }
224+ visited[i] = true; // Mark as visited
225+ int streakLength = 1; // Start streak length
226+ long j = (long) i * i; // Calculate square (use long to avoid overflow)
227+
228+ // Continue while j is valid and exists
229+ while (j >= 0 && j <= MAX_NUM && exists[(int) j]) {
230+ visited[(int) j] = true; // Mark as visited
231+ streakLength++; // Increase streak length
232+ j = j * j; // Move to next square
233+ }
234+
235+ // Update maximum streak if valid
236+ if (streakLength > 1) {
237+ maxStreak = Math.max(maxStreak, streakLength);
238+ }
239+ }
240+ return maxStreak; // Return the maximum streak found
241+ }
242+ }
243+ ```
244+
245+
121246#### C++
122247
123248``` cpp
@@ -200,38 +325,6 @@ class Solution:
200325 return - 1 if ans < 2 else ans
201326```
202327
203- #### Java
204-
205- ``` java
206- class Solution {
207- private Map<Integer , Integer > f = new HashMap<> ();
208- private Set<Integer > s = new HashSet<> ();
209-
210- public int longestSquareStreak (int [] nums ) {
211- for (int v : nums) {
212- s. add(v);
213- }
214- int ans = 0 ;
215- for (int v : nums) {
216- ans = Math . max(ans, dfs(v));
217- }
218- return ans < 2 ? - 1 : ans;
219- }
220-
221- private int dfs (int x ) {
222- if (! s. contains(x)) {
223- return 0 ;
224- }
225- if (f. containsKey(x)) {
226- return f. get(x);
227- }
228- int ans = 1 + dfs(x * x);
229- f. put(x, ans);
230- return ans;
231- }
232- }
233- ```
234-
235328#### C++
236329
237330``` cpp
0 commit comments