LeetCode 题解工作台
二的幂数组中查询范围内的乘积
给你一个正整数 n ,你需要找到一个下标从 0 开始的数组 powers ,它包含 最少 数目的 2 的幂,且它们的和为 n 。 powers 数组是 非递减 顺序的。根据前面描述,构造 powers 数组的方法是唯一的。 同时给你一个下标从 0 开始的二维整数数组 queries ,其中 quer…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 前缀和
答案摘要
我们可以使用位运算(Lowbit)来得到 数组,然后通过模拟的方式求出每个查询的答案。 首先,对于给定的正整数 ,我们通过 $n \& -n$ 可以快速得到二进制表示中最低位的 对应的数值,也就是当前数的最小 的幂因子。对 反复执行这个操作并减去该值,就能依次得到所有置位的 的幂,形成 数组。这个数组是递增的,且长度等于 的二进制表示中 的个数。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 前缀和 题型思路
题目描述
给你一个正整数 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 <= 1091 <= queries.length <= 1050 <= starti <= endi < powers.length
解题思路
方法一:位运算 + 模拟
我们可以使用位运算(Lowbit)来得到 数组,然后通过模拟的方式求出每个查询的答案。
首先,对于给定的正整数 ,我们通过 可以快速得到二进制表示中最低位的 对应的数值,也就是当前数的最小 的幂因子。对 反复执行这个操作并减去该值,就能依次得到所有置位的 的幂,形成 数组。这个数组是递增的,且长度等于 的二进制表示中 的个数。
接下来,我们需要处理每个查询。对于当前查询 ,我们需要计算
其中 是第 个查询的答案。由于查询的结果可能非常大,我们需要对每个答案取模 。
时间复杂度 ,其中 为数组 的长度。忽略答案的空间消耗,空间复杂度 。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(\log^2 n + q) |
| 空间 | O(\log^2 n) |
面试官常问的追问
外企场景- 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?
常见陷阱
外企场景- 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.
进阶变体
外企场景- 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.