LeetCode 题解工作台

二的幂数组中查询范围内的乘积

给你一个正整数 n ,你需要找到一个下标从 0 开始的数组 powers ,它包含 最少 数目的 2 的幂,且它们的和为 n 。 powers 数组是 非递减 顺序的。根据前面描述,构造 powers 数组的方法是唯一的。 同时给你一个下标从 0 开始的二维整数数组 queries ,其中 quer…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 前缀和

bolt

答案摘要

我们可以使用位运算(Lowbit)来得到 数组,然后通过模拟的方式求出每个查询的答案。 首先,对于给定的正整数 ,我们通过 $n \& -n$ 可以快速得到二进制表示中最低位的 对应的数值,也就是当前数的最小 的幂因子。对 反复执行这个操作并减去该值,就能依次得到所有置位的 的幂,形成 数组。这个数组是递增的,且长度等于 的二进制表示中 的个数。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 前缀和 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个正整数 n ,你需要找到一个下标从 0 开始的数组 powers ,它包含 最少 数目的 2 的幂,且它们的和为 n 。powers 数组是 非递减 顺序的。根据前面描述,构造 powers 数组的方法是唯一的。

同时给你一个下标从 0 开始的二维整数数组 queries ,其中 queries[i] = [lefti, righti] ,其中 queries[i] 表示请你求出满足 lefti <= j <= righti 的所有 powers[j] 的乘积。

请你返回一个数组 answers ,长度与 queries 的长度相同,其中 answers[i]是第 i 个查询的答案。由于查询的结果可能非常大,请你将每个 answers[i] 都对 109 + 7 取余 。

 

示例 1:

输入:n = 15, queries = [[0,1],[2,2],[0,3]]
输出:[2,4,64]
解释:
对于 n = 15 ,得到 powers = [1,2,4,8] 。没法得到元素数目更少的数组。
第 1 个查询的答案:powers[0] * powers[1] = 1 * 2 = 2 。
第 2 个查询的答案:powers[2] = 4 。
第 3 个查询的答案:powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64 。
每个答案对 109 + 7 取余得到的结果都相同,所以返回 [2,4,64] 。

示例 2:

输入:n = 2, queries = [[0,0]]
输出:[2]
解释:
对于 n = 2, powers = [2] 。
唯一一个查询的答案是 powers[0] = 2 。答案对 109 + 7 取余后结果相同,所以返回 [2] 。

 

提示:

  • 1 <= n <= 109
  • 1 <= queries.length <= 105
  • 0 <= starti <= endi < powers.length
lightbulb

解题思路

方法一:位运算 + 模拟

我们可以使用位运算(Lowbit)来得到 powers\textit{powers} 数组,然后通过模拟的方式求出每个查询的答案。

首先,对于给定的正整数 nn,我们通过 n&nn \& -n 可以快速得到二进制表示中最低位的 11 对应的数值,也就是当前数的最小 22 的幂因子。对 nn 反复执行这个操作并减去该值,就能依次得到所有置位的 22 的幂,形成 powers\textit{powers} 数组。这个数组是递增的,且长度等于 nn 的二进制表示中 11 的个数。

接下来,我们需要处理每个查询。对于当前查询 (l,r)(l, r),我们需要计算

answers[i]=j=lrpowers[j]\textit{answers}[i] = \prod_{j=l}^{r} \textit{powers}[j]

其中 answers[i]\textit{answers}[i] 是第 ii 个查询的答案。由于查询的结果可能非常大,我们需要对每个答案取模 109+710^9 + 7

时间复杂度 O(m×logn)O(m \times \log n),其中 mm 为数组 queries\textit{queries} 的长度。忽略答案的空间消耗,空间复杂度 O(logn)O(\log n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
        powers = []
        while n:
            x = n & -n
            powers.append(x)
            n -= x
        mod = 10**9 + 7
        ans = []
        for l, r in queries:
            x = 1
            for i in range(l, r + 1):
                x = x * powers[i] % mod
            ans.append(x)
        return ans
speed

复杂度分析

指标
时间O(\log^2 n + q)
空间O(\log^2 n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Does the candidate effectively apply bit manipulation to derive the powers array?

  • question_mark

    Can the candidate explain the trade-offs between using a brute force solution and an optimized approach with prefix products?

  • question_mark

    How well does the candidate handle large query sets and large values of n in terms of both time and space complexity?

warning

常见陷阱

外企场景
  • error

    Not using modular arithmetic during product calculations, leading to overflow errors.

  • error

    Inefficiently recalculating products for each query instead of utilizing a prefix product array.

  • error

    Failing to optimize for the constraints, particularly when handling a large number of queries or large values of n.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    The problem can be generalized to other operations like sum or minimum instead of product within a given range.

  • arrow_right_alt

    Instead of answering multiple queries, solve for a single query at a time in an optimized manner using bit manipulation.

  • arrow_right_alt

    Extend the problem to handle negative values of n by adjusting the binary representation approach.

help

常见问题

外企场景

二的幂数组中查询范围内的乘积题解:前缀和 | LeetCode #2438 中等