LeetCode 2501. Longest Square Streak in an Array Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2501. Longest Square Streak in an Array

Description

You are given an integer array nums. A subsequence of nums is called a square streak if:

  • The length of the subsequence is at least 2, and
  • after sorting the subsequence, each element (except the first element) is the square of the previous number.

Return the length of the longest square streak in nums, or return -1 if there is no square streak.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

 

Example 1:

Input: nums = [4,3,6,16,8,2]
Output: 3
Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
- 4 = 2 * 2.
- 16 = 4 * 4.
Therefore, [4,16,2] is a square streak.
It can be shown that every subsequence of length 4 is not a square streak.

Example 2:

Input: nums = [2,3,5,6,7]
Output: -1
Explanation: There is no square streak in nums so return -1.

 

Constraints:

  • 2 <= nums.length <= 105
  • 2 <= nums[i] <= 105

Solutions

Solution 1: Hash Table + Enumeration

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.

The time complexity is O(n × log log M), and the space complexity is O(n). Here, n is the length of the array nums, and M is the maximum value of the elements in the array nums.

PythonJavaC++GoTypeScriptJavaScript
class Solution: def longestSquareStreak(self, nums: List[int]) -> int: s = set(nums) ans = -1 for x in nums: t = 0 while x in s: x *= x t += 1 if t > 1: ans = max(ans, t) return ans(code-box)

Solution 2: Memoization Search

Similar to Solution 1, we first use a hash table to record all elements in the array. Then, we design a function dfs(x), which represents the length of the square wave starting with x. The answer is max(dfs(x)), where x is an element in the array nums.

The calculation process of the function dfs(x) is as follows:

  • If x is not in the hash table, return 0.
  • Otherwise, return 1 + dfs(x2).

During the process, we can use memoization, i.e., use a hash table to record the value of the function dfs(x) to avoid redundant calculations.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptJavaScript
class Solution: def longestSquareStreak(self, nums: List[int]) -> int: @cache def dfs(x: int) -> int: if x not in s: return 0 return 1 + dfs(x * x) s = set(nums) ans = max(dfs(x) for x in s) return -1 if ans < 2 else ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !