Skip to content

1. Two Sum#12

Merged
Ryotaro25 merged 6 commits intomainfrom
problem11
May 26, 2025
Merged

1. Two Sum#12
Ryotaro25 merged 6 commits intomainfrom
problem11

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/two-sum/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
Group Anagrams

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、loop.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

空間
O(n^2)

問題のジャンルがhash mapなのでmapを使った解法を考える
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@liquo-rice mapはhashではなく平衡二分探索木なのですね。。。🙇

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

mapはBSTなので、下の時間計算量は間違っています。

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.

@liquo-rice 指摘ありがとうございます。きちんと調べると計算量など違うのですね。
memo.mdも修正しました。
mapのcontainsを使う場合(log n)なので合計O(n log n)
https://en.cppreference.com/w/cpp/container/map/contains

unordered_mapのcontainsを使う場合はO(n)なので合計O(n^2)
https://en.cppreference.com/w/cpp/container/unordered_map/contains

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
map<int, int> numbers_to_index;
Copy link
Copy Markdown

@erutako erutako Jun 11, 2024

Choose a reason for hiding this comment

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

細かいですが、numbersは複数形でindexは単数形なのが気になりましたmm
個人的にはmapの一つのエントリを指す名前の方が直感的なので単数形でnum_to_indexとかが良さそうかなと思いましたmm

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.

@erutako
step5に追加しました。統一感の意識はしておかないとですね。。。
reflecting review

Comment on lines +7 to +8
for (int j = 0; j < numbers.size(); j++) {
if (i != j && target - numbers[i] - numbers[j] == 0) {
Copy link
Copy Markdown

@erutako erutako Jun 11, 2024

Choose a reason for hiding this comment

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

loopで書くなら、内側のfor文のカウント変数の初期化を int j = i + 1のようにして、全探索にしても余分な計算は省くとよいのかなと思いましたmm
これをすればif文の条件で i != jがいらなくなるので、そこも読みやすくなってメリットがあるかなと思います。

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.

@erutako
ありがとうございます!確かに初めからループさせる必要ないですね。。。
loop2.cppにて実装しました🙇
reflecting review

時間
O(n^2)
空間
O(n^2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

アルゴリズムにおいて追加で必要な空間という意味ではi, j, two_indicesぐらいなのでO(1)ではないでしょうか?
numbersの空間を計算量に入れたとしても、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.

@erutako レビューありがとうございます。誤っておりました。ご指摘のとおり、それぞれO(1)ですね。。。

reflecting reviewにて修正しました。

@Ryotaro25
Copy link
Copy Markdown
Owner Author

メモ
step4.cppについては身体性を意識して実装
https://discord.com/channels/1084280443945353267/1237649827240742942/1249892025948573706

vector<int> twoSum(vector<int>& numbers, int target) {

vector<int> two_indices;
for (int i = 0; i < numbers.size(); i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

結果は同じですが、i < numbers.size() - 1とした方が良さそうです。

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.

j = i + 1とするならこちらの方がいいですね。ループの回数もきちんと意識します。

vector<int> two_indices;
for (int i = 0; i < numbers.size(); i++) {
for (int j = i + 1; j < numbers.size(); j++) {
if (target - numbers[i] - numbers[j] == 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

普通はnumbers[i] + numbers[j] == targetとしませんか?

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.

@liquo-rice そうですね。他の解法で使ったremainingにつられてました。。。

if (target - numbers[i] - numbers[j] == 0) {
two_indices.push_back(i);
two_indices.push_back(j);
return two_indices;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

return {i, j}

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.

@liquo-rice
わざわざ変数に入れる必要ないですね。
下記にてloop3.cppを追加しました。
3bd35d9

@rihib rihib mentioned this pull request Aug 6, 2024
@Ryotaro25 Ryotaro25 merged commit 568329a into main May 26, 2025
1 check passed
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.

3 participants