Skip to content

Conversation

kazukiii
Copy link
Owner

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

次に解く問題
49. Group Anagrams

README.mdへ頭の中の言語化と記録をしています。

num_to_index = {}
for i in range(n):
complement = target - nums[i]
if complement in num_to_index:

Choose a reason for hiding this comment

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

やや冗長かもしれませんが、complement_valueのように、indexかvalueかをはっきりさせるとより良いかと思いました。remain_valueとかでも良いかもしれません。

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.

違うことをお伝えして申し訳ないのですが、自分はこのくらいのスコープならやっていることの全体がわかるのでcomplementは十分伝わるかなと思いました。
逆にcomplement_valueだと少し冗長に感じます。(好みの問題ですが...)

complementはこの問題の解答の中で一番わかり易い単語を使われているなと思いました!

Copy link

Choose a reason for hiding this comment

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

個人的にはこの流れで情報を足すならcomplement_numでしょうか。
num_to_indexという変数があるので、numかindexかをはっきりさせたいという感覚です。

Choose a reason for hiding this comment

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

erutakoさん
コメントありがとうございます。大きく違うことは言っていないと思っていて、自分も感覚としては、numかindexかをはっきりさせたいができれば良いかな、くらいのニュアンスでした。complement_valueは確かに全体のコード量を鑑みると冗長ですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

皆様コメントありがとうございます。
一応、自分の感覚もerutakoさんに近いです。最初に、complementと変数名を付けたときは Two's complementを連想していました。
調べてみたところ、この「補数」という単語は「ある数に対してその数を基準として加えることで特定の値(通常は0や2のべき乗)になる数のこと」という意味らしいので、注目している数(nums[i])に対するtargetの補数はvalueであることは比較的明確なのかもと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

と書きましたが、正解はなくどれも選択肢のうちの一つだと思うので考える機会を与えてくださりありがとうございます!

return [i, num_to_index[complement]]
num_to_index[nums[i]] = i

raise Exception()

Choose a reason for hiding this comment

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

raise Exception('unreachable')

のようなメッセージを付けると親切かなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにこちらの方が親切ですね。見直してみます。

def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
num_to_index = {}
for i in range(n):

Choose a reason for hiding this comment

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

for i, num in enumerate(nums):

という選択肢もありますね。

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
nums_with_index = [(nums[i], i) for i in range(n)]

Choose a reason for hiding this comment

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

上に、space: O(1)とありますが、この実装だとO(N)になりませんか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

O(1)は勘違いでした。この問題の場合、元のインデックスをどこかで保持する必要があるのでO(n)になりますね。。

num_to_index = {}
for i in range(n):
complement = target - nums[i]
if complement in num_to_index:

Choose a reason for hiding this comment

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

私も少し気になりました。

- ソートして二分探索
- ソートしたら二分探索可能
- time: O(n log n), space: O(1)
- ソートして2 pointers

Choose a reason for hiding this comment

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

これ、選択肢になかったです。
参考になりました。

return [i, num_to_index[complement]]
num_to_index[nums[i]] = i

raise Exception()
Copy link

Choose a reason for hiding this comment

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

認識されているかもしれませんが、他の選択肢もあります
https://discord.com/channels/1084280443945353267/1235971495696662578/1240719515722190982

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。確認しておきます。

- ハッシュマップ
- 追加のメモリを使用する
- 足し算が可換(答えの順番が関係ない)ので1パスでOK
- 引き算とかだとおそらく1パスではできない
Copy link

Choose a reason for hiding this comment

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

a が出てきたときに、x - a = target, a - x = target となるような x があったかを確認すればいいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに両方見れば大丈夫ですね!勉強になりました。

@rihib rihib mentioned this pull request Aug 6, 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.

6 participants