返回介绍

solution / 0500-0599 / 0565.Array Nesting / README_EN

发布于 2024-06-17 01:03:59 字数 5201 浏览 0 评论 0 收藏 0

565. Array Nesting

中文文档

Description

You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].

You should build a set s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... } subjected to the following rule:

  • The first element in s[k] starts with the selection of the element nums[k] of index = k.
  • The next element in s[k] should be nums[nums[k]], and then nums[nums[nums[k]]], and so on.
  • We stop adding right before a duplicate element occurs in s[k].

Return _the longest length of a set_ s[k].

 

Example 1:

Input: nums = [5,4,0,3,1,6,2]
Output: 4
Explanation: 
nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.
One of the longest sets s[k]:
s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}

Example 2:

Input: nums = [0,1,2]
Output: 1

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] < nums.length
  • All the values of nums are unique.

Solutions

Solution 1

class Solution:
  def arrayNesting(self, nums: List[int]) -> int:
    n = len(nums)
    vis = [False] * n
    res = 0
    for i in range(n):
      if vis[i]:
        continue
      cur, m = nums[i], 1
      vis[cur] = True
      while nums[cur] != nums[i]:
        cur = nums[cur]
        m += 1
        vis[cur] = True
      res = max(res, m)
    return res
class Solution {
  public int arrayNesting(int[] nums) {
    int n = nums.length;
    boolean[] vis = new boolean[n];
    int res = 0;
    for (int i = 0; i < n; i++) {
      if (vis[i]) {
        continue;
      }
      int cur = nums[i], m = 1;
      vis[cur] = true;
      while (nums[cur] != nums[i]) {
        cur = nums[cur];
        m++;
        vis[cur] = true;
      }
      res = Math.max(res, m);
    }
    return res;
  }
}
class Solution {
public:
  int arrayNesting(vector<int>& nums) {
    int n = nums.size();
    vector<bool> vis(n);
    int res = 0;
    for (int i = 0; i < n; ++i) {
      if (vis[i]) continue;
      int cur = nums[i], m = 1;
      vis[cur] = true;
      while (nums[cur] != nums[i]) {
        cur = nums[cur];
        ++m;
        vis[cur] = true;
      }
      res = max(res, m);
    }
    return res;
  }
};
func arrayNesting(nums []int) int {
  n := len(nums)
  vis := make([]bool, n)
  ans := 0
  for i := 0; i < n; i++ {
    if vis[i] {
      continue
    }
    cur, m := nums[i], 1
    vis[cur] = true
    for nums[cur] != nums[i] {
      cur = nums[cur]
      m++
      vis[cur] = true
    }
    if m > ans {
      ans = m
    }
  }
  return ans
}

Solution 2

class Solution:
  def arrayNesting(self, nums: List[int]) -> int:
    ans, n = 0, len(nums)
    for i in range(n):
      cnt = 0
      while nums[i] != n:
        j = nums[i]
        nums[i] = n
        i = j
        cnt += 1
      ans = max(ans, cnt)
    return ans
class Solution {
  public int arrayNesting(int[] nums) {
    int ans = 0, n = nums.length;
    for (int i = 0; i < n; ++i) {
      int cnt = 0;
      int j = i;
      while (nums[j] < n) {
        int k = nums[j];
        nums[j] = n;
        j = k;
        ++cnt;
      }
      ans = Math.max(ans, cnt);
    }
    return ans;
  }
}
class Solution {
public:
  int arrayNesting(vector<int>& nums) {
    int ans = 0, n = nums.size();
    for (int i = 0; i < n; ++i) {
      int cnt = 0;
      int j = i;
      while (nums[j] < n) {
        int k = nums[j];
        nums[j] = n;
        j = k;
        ++cnt;
      }
      ans = max(ans, cnt);
    }
    return ans;
  }
};
func arrayNesting(nums []int) int {
  ans, n := 0, len(nums)
  for i := range nums {
    cnt, j := 0, i
    for nums[j] != n {
      k := nums[j]
      nums[j] = n
      j = k
      cnt++
    }
    if ans < cnt {
      ans = cnt
    }
  }
  return ans
}

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

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

发布评论

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