返回介绍

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

发布于 2024-06-17 01:03:04 字数 5618 浏览 0 评论 0 收藏 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

class Solution:
  def longestSquareStreak(self, nums: List[int]) -> int:
    s = set(nums)
    ans = -1
    for v in nums:
      t = 0
      while v in s:
        v *= v
        t += 1
      if t > 1:
        ans = max(ans, t)
    return ans
class Solution {
  public int longestSquareStreak(int[] nums) {
    Set<Integer> s = new HashSet<>();
    for (int v : nums) {
      s.add(v);
    }
    int ans = -1;
    for (int v : nums) {
      int t = 0;
      while (s.contains(v)) {
        v *= v;
        ++t;
      }
      if (t > 1) {
        ans = Math.max(ans, t);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int longestSquareStreak(vector<int>& nums) {
    unordered_set<long long> s(nums.begin(), nums.end());
    int ans = -1;
    for (int& v : nums) {
      int t = 0;
      long long x = v;
      while (s.count(x)) {
        x *= x;
        ++t;
      }
      if (t > 1) ans = max(ans, t);
    }
    return ans;
  }
};
func longestSquareStreak(nums []int) int {
  s := map[int]bool{}
  for _, v := range nums {
    s[v] = true
  }
  ans := -1
  for _, v := range nums {
    t := 0
    for s[v] {
      v *= v
      t++
    }
    if t > 1 && t > ans {
      ans = t
    }
  }
  return ans
}

Solution 2

class Solution:
  def longestSquareStreak(self, nums: List[int]) -> int:
    @cache
    def dfs(x):
      if x not in s:
        return 0
      return 1 + dfs(x * x)

    s = set(nums)
    ans = max(dfs(x) for x in nums)
    return -1 if ans < 2 else ans
class Solution {
  private Map<Integer, Integer> f = new HashMap<>();
  private Set<Integer> s = new HashSet<>();

  public int longestSquareStreak(int[] nums) {
    for (int v : nums) {
      s.add(v);
    }
    int ans = 0;
    for (int v : nums) {
      ans = Math.max(ans, dfs(v));
    }
    return ans < 2 ? -1 : ans;
  }

  private int dfs(int x) {
    if (!s.contains(x)) {
      return 0;
    }
    if (f.containsKey(x)) {
      return f.get(x);
    }
    int ans = 1 + dfs(x * x);
    f.put(x, ans);
    return ans;
  }
}
class Solution {
public:
  int longestSquareStreak(vector<int>& nums) {
    unordered_set<long long> s(nums.begin(), nums.end());
    int ans = 0;
    unordered_map<int, int> f;
    function<int(int)> dfs = [&](int x) -> int {
      if (!s.count(x)) return 0;
      if (f.count(x)) return f[x];
      long long t = 1ll * x * x;
      if (t > INT_MAX) return 1;
      f[x] = 1 + dfs(x * x);
      return f[x];
    };
    for (int& v : nums) ans = max(ans, dfs(v));
    return ans < 2 ? -1 : ans;
  }
};
func longestSquareStreak(nums []int) (ans int) {
  s := map[int]bool{}
  for _, v := range nums {
    s[v] = true
  }
  f := map[int]int{}
  var dfs func(int) int
  dfs = func(x int) int {
    if !s[x] {
      return 0
    }
    if v, ok := f[x]; ok {
      return v
    }
    f[x] = 1 + dfs(x*x)
    return f[x]
  }
  for _, v := range nums {
    if t := dfs(v); ans < t {
      ans = t
    }
  }
  if ans < 2 {
    return -1
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文