首页 > web前端 > js教程 > 正文

Typescript 编码编年史:除 Self 之外的数组的乘积

王林
发布: 2024-07-17 11:38:48
原创
628 人浏览过

Typescript Coding Chronicles: Product of Array Except Self

问题陈述:

给定一个整数数组 nums,返回一个数组 answer,使得 answer[i] 等于 nums 中除 nums[i] 之外的所有元素的乘积。

nums 的任何前缀或后缀的乘积保证适合 32 位整数。

您必须编写一个在 O(n) 时间内运行且不使用除法运算的算法。

示例1:

  • 输入:nums = [1,2,3,4]
  • 输出:[24,12,8,6]

示例2:

  • 输入:nums = [-1,1,0,-3,3]
  • 输出:[0,0,9,0,0]

限制条件:

  • 2
  • -30
  • nums 的任何前缀或后缀的乘积保证适合 32 位整数。

后续:

你能以 O(1) 额外的空间复杂度解决这个问题吗? (输出数组不计为空间复杂度分析的额外空间。)

初步思考过程:

为了解决这个问题,我们需要计算除当前元素之外的所有元素的乘积,而不使用除法运算。这可以通过对数组使用两次传递来完成:

  1. 计算每个元素的前缀积。
  2. 计算每个元素的后缀乘积并与前缀乘积相乘。

基本解决方案:

我们可以使用两个数组来存储前缀和后缀的乘积,然后将它们相乘得到最终结果。

代码:

function productExceptSelf(nums: number[]): number[] {
    const n = nums.length;
    const prefixProducts = new Array(n).fill(1);
    const suffixProducts = new Array(n).fill(1);
    const result = new Array(n).fill(1);

    // Compute prefix products
    for (let i = 1; i < n; i++) {
        prefixProducts[i] = prefixProducts[i - 1] * nums[i - 1];
    }

    // Compute suffix products
    for (let i = n - 2; i >= 0; i--) {
        suffixProducts[i] = suffixProducts[i + 1] * nums[i + 1];
    }

    // Compute the result by multiplying prefix and suffix products
    for (let i = 0; i < n; i++) {
        result[i] = prefixProducts[i] * suffixProducts[i];
    }

    return result;
}
登录后复制

时间复杂度分析:

  • 时间复杂度: O(n),其中 n 是数组的长度。我们迭代数组三次。
  • 空间复杂度: O(n),用于存储前缀和后缀乘积。

限制:

基本解决方案效果很好,但需要额外的空间来存储前缀和后缀产品。

优化方案:

我们可以优化解决方案以使用 O(1) 额外空间,方法是使用输出数组首先存储前缀产品,然后就地修改它以包含后缀产品。

代码:

function productExceptSelfOptimized(nums: number[]): number[] {
    const n = nums.length;
    const result = new Array(n).fill(1);

    // Compute prefix products in the result array
    for (let i = 1; i < n; i++) {
        result[i] = result[i - 1] * nums[i - 1];
    }

    // Compute suffix products and multiply with the prefix products
    let suffixProduct = 1;
    for (let i = n - 1; i >= 0; i--) {
        result[i] = result[i] * suffixProduct;
        suffixProduct *= nums[i];
    }

    return result;
}
登录后复制

时间复杂度分析:

  • 时间复杂度: O(n),其中 n 是数组的长度。我们迭代数组两次。
  • 空间复杂度: O(1),因为我们使用输出数组来存储中间结果,而不使用任何额外的空间。

基本解决方案的改进:

  • 优化的解决方案通过使用输出数组作为中间结果,将空间复杂度降低到 O(1)。

边缘情况和测试:

边缘情况:

  1. 数组包含零。
  2. 数组包含负数。
  3. 数组长度是最小(2)或最大(10^5)限制。

测试用例:

console.log(productExceptSelf([1,2,3,4])); // [24,12,8,6]
console.log(productExceptSelf([-1,1,0,-3,3])); // [0,0,9,0,0]
console.log(productExceptSelf([2,2,2,2])); // [8,8,8,8]
console.log(productExceptSelf([0,0])); // [0,0]
console.log(productExceptSelf([5])); // This should not be a valid input as the minimum length is 2
console.log(productExceptSelf([1,2])); // [2, 1]

console.log(productExceptSelfOptimized([1,2,3,4])); // [24,12,8,6]
console.log(productExceptSelfOptimized([-1,1,0,-3,3])); // [0,0,9,0,0]
console.log(productExceptSelfOptimized([2,2,2,2])); // [8,8,8,8]
console.log(productExceptSelfOptimized([0,0])); // [0,0]
console.log(productExceptSelfOptimized([5])); // This should not be a valid input as the minimum length is 2
console.log(productExceptSelfOptimized([1,2])); // [2, 1]
登录后复制

一般解决问题的策略:

  1. 理解问题:仔细阅读问题陈述,了解要求和约束。
  2. 识别关键操作:确定需要的关键操作,例如计算前缀和后缀乘积。
  3. 优化可读性:使用清晰简洁的逻辑来确保代码易于理解。
  4. 彻底测试:使用各种情况(包括边缘情况)测试解决方案,以确保正确性。

识别类似问题:

  1. 前缀和数组:

    • 需要计算范围查询的前缀和的问题。
    • 示例:范围求和查询。
  2. 就地算法:

    • 需要在有限的额外空间内进行操作的问题。
    • 示例:将数组向右旋转 k 步。
  3. 数组操作:

    • 需要根据特定条件修改数组的问题。
    • 示例:将零移至数组末尾。

结论:

  • 使用额外空间的基本方法和优化的就地方法可以有效地解决计算除 self 之外的数组的乘积的问题。
  • 理解问题并将其分解为可管理的部分至关重要。
  • 使用清晰的逻辑并优化可读性可确保解决方案易于理解。
  • 使用各种边缘情况进行测试可确保稳健性。
  • 认识问题的模式有助于将类似的解决方案应用于其他挑战。

通过练习这些问题和策略,您可以提高解决问题的能力,并为各种编码挑战做好更好的准备。

以上是Typescript 编码编年史:除 Self 之外的数组的乘积的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!