Skip to content

238. Product of Array Except Self#5

Merged
t-ooka merged 9 commits intomainfrom
question/Product-of-Array-Except-Self
Jul 2, 2024
Merged

238. Product of Array Except Self#5
t-ooka merged 9 commits intomainfrom
question/Product-of-Array-Except-Self

Conversation

@t-ooka
Copy link
Copy Markdown
Owner

@t-ooka t-ooka commented Jun 5, 2024

問題リンク

問題文

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:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]

Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]

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.

```

-時間計算量 o(n)
-空間計算量 o(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res = [0] * len(nums) としているので、空間計算量は O(n) ではないでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レスポンスとして使用する値については空間計算量に含まない認識で書いておりました。
ソースコードベースですとおっしゃるとおり、O(n)です、、!


left,rightはそれぞれ、各々のインデックスの数までの積を表しているので、両隣の数の積を取れば自身をのぞいた積にになる。

- 時間計算量 o(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ビッグオー記法は o ではなく O を使うことをお勧めいたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解しました。以後Oで統一します。

vector<int> productExceptSelf(vector<int>& nums) {
int length = nums.size();

vector<int> L(length);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ローカル変数の名前は snake_case で命名することをお勧めします。
https://google.github.io/styleguide/cppguide.html#Variable_Names

The names of variables (including function parameters) and data members are snake_case (all lowercase, with underscores between words).

また、変数名には変数の値の目的や意図を端的に表した英単語・英語句を付けることをお勧めします。
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules

Use names that describe the purpose or intent of the object.

}

vector<int> R(length);
R[length-1] = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 等、 2 項演算子の左右に空白を一つずつ空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

Other binary operators usually have spaces around them,

R[i] = R[i+1] * nums[i+1];
}

vector<int> ans(length);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数名には原則省略形は用いないことをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules

Minimize the use of abbreviations that would likely be unknown to someone outside your project (especially acronyms and initialisms). Do not abbreviate by deleting letters within a word. As a rule of thumb, an abbreviation is probably OK if it's listed in Wikipedia. Generally speaking, descriptiveness should be proportional to the name's scope of visibility.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

わかりました。省略形は極力避けるようにいたします。

@t-ooka
Copy link
Copy Markdown
Owner Author

t-ooka commented Jul 2, 2024

数週間空けて解けたのでマージします。

@t-ooka t-ooka merged commit afa2b54 into main Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants