Skip to content

Commit dab4d71

Browse files
committed
Add or modify solutions: feat: add/update solution(s) to lc problem(s): No.2501
1 parent c90d23a commit dab4d71

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
private Map<Integer, Integer> memo = new HashMap<>(); // Memoization map
3-
private Set<Integer> numSet = new HashSet<>(); // Set of numbers
3+
private Set<Integer> numSet = new HashSet<>(); // Set of numbers
44

55
public int longestSquareStreak(int[] nums) {
66
// Early exit if the array length is less than 2
@@ -33,16 +33,16 @@ private int dfs(int x) {
3333
}
3434

3535
// Calculate the next number in the square streak
36-
long next = (long) x * x; // Use long to avoid overflow
36+
long next = (long)x * x; // Use long to avoid overflow
3737
// If the next number exceeds the maximum allowed value, return 0
3838
if (next > 100000) {
3939
memo.put(x, 1); // Only count this number itself
4040
return 1;
4141
}
4242

4343
// 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
44+
int streak = 1 + dfs((int)next); // Include the current number in the streak
45+
memo.put(x, streak); // Cache the result
4646
return streak;
4747
}
4848
}

solution/2500-2599/2501.Longest Square Streak in an Array/Solution4.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
public class Solution {
22
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
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
66
boolean[] visited = new boolean[MAX_NUM + 1]; // Track visited numbers
77

88
// Mark existing numbers
@@ -15,15 +15,15 @@ public int longestSquareStreak(int[] nums) {
1515
if (!exists[i] || visited[i]) {
1616
continue; // Skip if number doesn't exist or is already visited
1717
}
18-
visited[i] = true; // Mark as visited
18+
visited[i] = true; // Mark as visited
1919
int streakLength = 1; // Start streak length
20-
long j = (long) i * i; // Calculate square (use long to avoid overflow)
20+
long j = (long)i * i; // Calculate square (use long to avoid overflow)
2121

2222
// 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
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
2727
}
2828

2929
// Update maximum streak if valid

0 commit comments

Comments
 (0)