LeetCode 题解工作台

根据质数下标分割数组

给你一个整数数组 nums 。 根据以下规则将 nums 分割成两个数组 A 和 B : nums 中位于 质数 下标的元素必须放入数组 A 。 所有其他元素必须放入数组 B 。 返回两个数组和的 绝对 差值: |sum(A) - sum(B)| 。 质数 是一个大于 1 的自然数,它只有两个因子,…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·数学

bolt

答案摘要

我们可以用埃氏筛法预处理出 $[0, 10^5]$ 范围内的所有质数。然后遍历数组 $ \textit{nums}$,对于 ,如果 是质数,则将 加到答案中,否则将 加到答案中。最后返回答案的绝对值。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums

根据以下规则将 nums 分割成两个数组 AB

  • nums 中位于 质数 下标的元素必须放入数组 A
  • 所有其他元素必须放入数组 B

返回两个数组和的 绝对 差值:|sum(A) - sum(B)|

质数 是一个大于 1 的自然数,它只有两个因子,1和它本身。

注意:空数组的和为 0。

 

示例 1:

输入: nums = [2,3,4]

输出: 1

解释:

  • 数组中唯一的质数下标是 2,所以 nums[2] = 4 被放入数组 A
  • 其余元素 nums[0] = 2nums[1] = 3 被放入数组 B
  • sum(A) = 4sum(B) = 2 + 3 = 5
  • 绝对差值是 |4 - 5| = 1

示例 2:

输入: nums = [-1,5,7,0]

输出: 3

解释:

  • 数组中的质数下标是 2 和 3,所以 nums[2] = 7nums[3] = 0 被放入数组 A
  • 其余元素 nums[0] = -1nums[1] = 5 被放入数组 B
  • sum(A) = 7 + 0 = 7sum(B) = -1 + 5 = 4
  • 绝对差值是 |7 - 4| = 3

 

提示:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
lightbulb

解题思路

方法一:埃氏筛 + 模拟

我们可以用埃氏筛法预处理出 [0,105][0, 10^5] 范围内的所有质数。然后遍历数组 nums \textit{nums},对于 nums[i]\textit{nums}[i],如果 ii 是质数,则将 nums[i]\textit{nums}[i] 加到答案中,否则将 nums[i]-\textit{nums}[i] 加到答案中。最后返回答案的绝对值。

忽略预处理的时间和空间,时间复杂度 O(n)O(n),其中 nn 是数组 nums\textit{nums} 的长度,空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
m = 10**5 + 10
primes = [True] * m
primes[0] = primes[1] = False
for i in range(2, m):
    if primes[i]:
        for j in range(i + i, m, i):
            primes[j] = False


class Solution:
    def splitArray(self, nums: List[int]) -> int:
        return abs(sum(x if primes[i] else -x for i, x in enumerate(nums)))
speed

复杂度分析

指标
时间complexity is O(n log log n) for the sieve plus O(n) for iterating the array, totaling O(n log log n). Space complexity is O(n) for the prime index mask, but the algorithm can reduce auxiliary storage by summing on the fly.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if you can optimize by avoiding repeated prime checks per index.

  • question_mark

    Be ready to handle negative and large values while calculating sums.

  • question_mark

    Consider edge cases where nums.length is very small or consists entirely of primes.

warning

常见陷阱

外企场景
  • error

    Confusing array indices with element values when separating by prime indices.

  • error

    Failing to account for the first index being 0 or 1, which are not prime.

  • error

    Using inefficient prime checks instead of a sieve, leading to timeouts.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Split array using odd or even indices instead of prime indices for a simpler variant.

  • arrow_right_alt

    Compute maximum absolute difference rather than minimum, testing sum strategy changes.

  • arrow_right_alt

    Use a sliding window of prime indices to handle dynamic array updates efficiently.

help

常见问题

外企场景

根据质数下标分割数组题解:数组·数学 | LeetCode #3618 中等