Skip to content

1. Two Sum #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

1. Two Sum #11

wants to merge 1 commit into from

Conversation

potrue
Copy link
Owner

@potrue potrue commented May 13, 2025

enumerate()が頭から抜け落ちていた。
それと、どうせ後ろの方のindexを作るときにチェックできるので、最初に全部入れてしまう必要はなかった。

当てはまるものがなかった場合のreturnをどうするかはいろいろ派閥がありそう。一応List[int]を返すということになっているので[]を返すことにした。

Choose a reason for hiding this comment

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

同じ値が複数ある場合や、答えとなる組がたくさんある場合の挙動がどうなるか、という観点もありますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

個人的には一つ見つかった時点で返すのであればそもそも関数名をfirstTwoSumのような感じにしたい気持ちがありますね!

## 何も見ずに解いてみる

一回数字からindexを対応づけるようなdictを作ってしまって、差となる数字に別のindexが存在するならそれを返すとすれば時間計算量O(n)でいけそう。
メモリの消費がちょっと多くなってしまうのが難点ではある。

Choose a reason for hiding this comment

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

どの程度多くなってしまいますか、それはどの程度難点でしょうか、他の解決法はありますか、と聞かれそうです。(私はわかっていません)

Copy link
Owner Author

Choose a reason for hiding this comment

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

自分は普段空間計算量はぶっちゃけほとんど気にしないでコーディングしてしまっているのですが、実際仕事で使う人はどれぐらい気にしていらっしゃるんですかね。。。(もちろんプログラムを動かすシステムが何かにもよると思いますが)

Copy link

Choose a reason for hiding this comment

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

あまり気にしないですが、見積もれるようにはしておきましょう。計算量ではなくて、だいたいどれくらい使うかをバイトで計算しましょう。

アルゴリズムは常に定数倍が重要です。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.1itsm36fdjze

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