スポンサーリンク

【LeetCode】238. Product of Array Except Self 解答・解説【Python】

スポンサーリンク
スポンサーリンク
この記事は約6分で読めます。

 

はじめに

LeetCodeの問題を解答します。

なるべく、問題の和訳と詳細なコメントを書いています。

余裕があれば、複数のアプローチの解答と、実際の面接を想定して英語での解法やコメントを書いています。

 

これまでこのサイトでメモしてきた問題はこのページに全て載せています。

LeetCode 解答・解説記事一覧

 

二分探索、連結リストなど、テーマを絞って集中的に解きたい方は以下の記事が有用です。

LeeetCodeの問題をアルゴリズムとデータ構造による分類しました。

 

また、LeetCodeではAtcoderと違ってクラスと関数を定義して解答します。

LeetCodeに特有の内容など、知っておくと役に立つかもしれないことをまとめています。

はじめてLeetCodeに触れる方はこちらの記事も役に立つと思います。

解答前に知っておくと役に立つかもしれない情報

 

 

詳細

 

問題

 

原文

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

 

Example 1:

Example 2:

 

Constraints:

  • 2 <= nums.length <= 105
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

 

Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

 

 

内容(和訳)

整数配列numsが与えられます。i番目の要素がそれ以外の全要素の掛算である配列を返してください。任意のnumsの接頭辞または接尾辞の積は、32ビット整数に収まることが保証されています。

O(n)の時間内に動作し、除算演算を使用しないアルゴリズムを書く必要があります。

 

※正しくない可能性があります。

 

考察

2重ループで掛算をするとシンプルでわかりやすいですが、問題の指定にあるO(n)の時間計算量には収まりません。O(n)で解答するには、最初に全ての掛算をしておき、後から各要素ごとに割り算した値を解答用のリストに保存しておく必要があります。

ただし、0が含まれている場合の扱いに注意が必要です。

0が2つあれば、全ての要素は0を持つリストを返せばよく、0が一つだけの場合は、それ以外の要素は0、0の時だけはそれ以外の全要素の掛算の結果を解答として返す必要があります。

[1,2,0,3]の場合→[0,0,6,0]が解答

[1,0,0,4]の場合→[0,0,0,0]が解答

 

以下は英語面接をイメージして記載していたコメントです。

“””
About question
    I need to answer the array.
    The array has element that has the result of product of all element except i-th element.
Assumption
    1.Length of nums is at least one
    2.nums is from -100 to 100
Test cases
    1.general
        [1,2,3,4]
        [24,12,8,6]
    2.general
        [0,-1,4,9,-45]
        []
    3.edge case
        [1]
        []
    4.edge case
        [0,0,0]
        [0,0,0]
Approach
    1.Brute Force
        1.Perfome for loop iterate over each element.
        2.product all elements except i-th element
        Time complexity is Big O of n^2.
        Space complexity is Big O of n.
    2.
        1.calculate product of each element
        2.Performe for loop.
          each iterate the result of all elements is divided by i-th element
          and append to the array to return.
        3.Return the array.
        Time complexity is Big O of n.
        Space complexity is Big O of n.
“””

 

解答

 

解答1:Brute Force

これはLeetCodeでは時間オーバーになります。

 

解答2:最初に全要素の掛算を行い、後から要素ごとに割り算

解答3: Python, 英語コメント

 

解答の考え方は解答2と同じ。

実際の英語の面接をイメージして、議論すべきことを最初に書いてみた

 

 

終わりに

補足・参考・感想

 

問題を分類しました。テーマごとに集中して問題を解くことができます。

LeeetCodeの問題をアルゴリズムとデータ構造による分類

 

LeetCodeに特有の内容など、知っておくと役に立つかもしれないことをまとめました。

解答前に知っておくと役に立つかもしれない情報

 

 

他の問題もどうぞ

 

前:

 

次:

 

LeetCode 解答・解説記事一覧

 

コメント