Skip to content

Commit fbb4216

Browse files
committed
added 2 java solutions which works on all updated test cases.
1 parent e307732 commit fbb4216

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
private Map<Integer, Integer> memo = new HashMap<>(); // Memoization map
3+
private Set<Integer> numSet = new HashSet<>(); // Set of numbers
4+
5+
public int longestSquareStreak(int[] nums) {
6+
// Early exit if the array length is less than 2
7+
if (nums.length < 2)
8+
return -1;
9+
10+
// Add numbers to the set for quick lookup
11+
for (int n : nums) {
12+
numSet.add(n);
13+
}
14+
15+
int maxStreak = 0; // Track the maximum streak
16+
// Calculate the longest square streak for each number
17+
for (int n : nums) {
18+
maxStreak = Math.max(maxStreak, dfs(n));
19+
}
20+
21+
return maxStreak < 2 ? -1 : maxStreak; // Return result
22+
}
23+
24+
private int dfs(int x) {
25+
// Base case: if x is not in the set, return 0
26+
if (!numSet.contains(x)) {
27+
return 0;
28+
}
29+
30+
// Return cached result if already computed
31+
if (memo.containsKey(x)) {
32+
return memo.get(x);
33+
}
34+
35+
// Calculate the next number in the square streak
36+
long next = (long) x * x; // Use long to avoid overflow
37+
// If the next number exceeds the maximum allowed value, return 0
38+
if (next > 100000) {
39+
memo.put(x, 1); // Only count this number itself
40+
return 1;
41+
}
42+
43+
// Recursively calculate the streak
44+
int streak = 1 + dfs((int) next); // Include the current number in the streak
45+
memo.put(x, streak); // Cache the result
46+
return streak;
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
public int longestSquareStreak(int[] nums) {
3+
int maxStreak = -1; // Track the maximum streak
4+
final int MAX_NUM = (int) Math.pow(10, 5); // Maximum value for nums
5+
boolean[] exists = new boolean[MAX_NUM + 1]; // Track existence of numbers
6+
boolean[] visited = new boolean[MAX_NUM + 1]; // Track visited numbers
7+
8+
// Mark existing numbers
9+
for (int num : nums) {
10+
exists[num] = true;
11+
}
12+
13+
// Check for square streaks
14+
for (int i = 2; i * i <= MAX_NUM; i++) {
15+
if (!exists[i] || visited[i]) {
16+
continue; // Skip if number doesn't exist or is already visited
17+
}
18+
visited[i] = true; // Mark as visited
19+
int streakLength = 1; // Start streak length
20+
long j = (long) i * i; // Calculate square (use long to avoid overflow)
21+
22+
// Continue while j is valid and exists
23+
while (j >= 0 && j <= MAX_NUM && exists[(int) j]) {
24+
visited[(int) j] = true; // Mark as visited
25+
streakLength++; // Increase streak length
26+
j = j * j; // Move to next square
27+
}
28+
29+
// Update maximum streak if valid
30+
if (streakLength > 1) {
31+
maxStreak = Math.max(maxStreak, streakLength);
32+
}
33+
}
34+
return maxStreak; // Return the maximum streak found
35+
}
36+
}

0 commit comments

Comments
 (0)