返回介绍

solution / 2500-2599 / 2584.Split the Array to Make Coprime Products / README_EN

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

2584. Split the Array to Make Coprime Products

中文文档

Description

You are given a 0-indexed integer array nums of length n.

A split at an index i where 0 <= i <= n - 2 is called valid if the product of the first i + 1 elements and the product of the remaining elements are coprime.

  • For example, if nums = [2, 3, 3], then a split at the index i = 0 is valid because 2 and 9 are coprime, while a split at the index i = 1 is not valid because 6 and 3 are not coprime. A split at the index i = 2 is not valid because i == n - 1.

Return _the smallest index _i_ at which the array can be split validly or _-1_ if there is no such split_.

Two values val1 and val2 are coprime if gcd(val1, val2) == 1 where gcd(val1, val2) is the greatest common divisor of val1 and val2.

 

Example 1:

Input: nums = [4,7,8,15,3,5]
Output: 2
Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
The only valid split is at index 2.

Example 2:

Input: nums = [4,7,15,8,3,5]
Output: -1
Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
There is no valid split.

 

Constraints:

  • n == nums.length
  • 1 <= n <= 104
  • 1 <= nums[i] <= 106

Solutions

Solution 1

class Solution:
  def findValidSplit(self, nums: List[int]) -> int:
    first = {}
    n = len(nums)
    last = list(range(n))
    for i, x in enumerate(nums):
      j = 2
      while j <= x // j:
        if x % j == 0:
          if j in first:
            last[first[j]] = i
          else:
            first[j] = i
          while x % j == 0:
            x //= j
        j += 1
      if x > 1:
        if x in first:
          last[first[x]] = i
        else:
          first[x] = i
    mx = last[0]
    for i, x in enumerate(last):
      if mx < i:
        return mx
      mx = max(mx, x)
    return -1
class Solution {
  public int findValidSplit(int[] nums) {
    Map<Integer, Integer> first = new HashMap<>();
    int n = nums.length;
    int[] last = new int[n];
    for (int i = 0; i < n; ++i) {
      last[i] = i;
    }
    for (int i = 0; i < n; ++i) {
      int x = nums[i];
      for (int j = 2; j <= x / j; ++j) {
        if (x % j == 0) {
          if (first.containsKey(j)) {
            last[first.get(j)] = i;
          } else {
            first.put(j, i);
          }
          while (x % j == 0) {
            x /= j;
          }
        }
      }
      if (x > 1) {
        if (first.containsKey(x)) {
          last[first.get(x)] = i;
        } else {
          first.put(x, i);
        }
      }
    }
    int mx = last[0];
    for (int i = 0; i < n; ++i) {
      if (mx < i) {
        return mx;
      }
      mx = Math.max(mx, last[i]);
    }
    return -1;
  }
}
class Solution {
public:
  int findValidSplit(vector<int>& nums) {
    unordered_map<int, int> first;
    int n = nums.size();
    vector<int> last(n);
    iota(last.begin(), last.end(), 0);
    for (int i = 0; i < n; ++i) {
      int x = nums[i];
      for (int j = 2; j <= x / j; ++j) {
        if (x % j == 0) {
          if (first.count(j)) {
            last[first[j]] = i;
          } else {
            first[j] = i;
          }
          while (x % j == 0) {
            x /= j;
          }
        }
      }
      if (x > 1) {
        if (first.count(x)) {
          last[first[x]] = i;
        } else {
          first[x] = i;
        }
      }
    }
    int mx = last[0];
    for (int i = 0; i < n; ++i) {
      if (mx < i) {
        return mx;
      }
      mx = max(mx, last[i]);
    }
    return -1;
  }
};
func findValidSplit(nums []int) int {
  first := map[int]int{}
  n := len(nums)
  last := make([]int, n)
  for i := range last {
    last[i] = i
  }
  for i, x := range nums {
    for j := 2; j <= x/j; j++ {
      if x%j == 0 {
        if k, ok := first[j]; ok {
          last[k] = i
        } else {
          first[j] = i
        }
        for x%j == 0 {
          x /= j
        }
      }
    }
    if x > 1 {
      if k, ok := first[x]; ok {
        last[k] = i
      } else {
        first[x] = i
      }
    }
  }
  mx := last[0]
  for i, x := range last {
    if mx < i {
      return mx
    }
    mx = max(mx, x)
  }
  return -1
}

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

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

发布评论

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