-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 153 ms (6.25%), Space: 64.6 MB (5.56%) - LeetHub
- Loading branch information
1 parent
09e956f
commit e552a99
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
2501-longest-square-streak-in-an-array/2501-longest-square-streak-in-an-array.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public int longestSquareStreak(int[] nums) { | ||
TreeSet<Integer> set = new TreeSet<>(); | ||
for(int num : nums) { | ||
set.add(num); | ||
} | ||
|
||
int ans = 1; | ||
while(!set.isEmpty()) { | ||
int curr = set.first(); | ||
int streak = 0; | ||
while(!set.isEmpty() && set.contains(curr)) { | ||
set.remove(curr); | ||
curr = curr * curr; | ||
streak++; | ||
} | ||
ans = Math.max(streak, ans); | ||
} | ||
return ans == 1 ? -1 : ans; | ||
} | ||
} |