-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #12
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
base: main
Are you sure you want to change the base?
1. Two Sum #12
Conversation
num_to_index = {} | ||
for i in range(n): | ||
complement = target - nums[i] | ||
if complement in num_to_index: |
There was a problem hiding this comment.
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とかでも良いかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私も少し気になりました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。変数名見直してみます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
違うことをお伝えして申し訳ないのですが、自分はこのくらいのスコープならやっていることの全体がわかるのでcomplement
は十分伝わるかなと思いました。
逆にcomplement_value
だと少し冗長に感じます。(好みの問題ですが...)
complement
はこの問題の解答の中で一番わかり易い単語を使われているなと思いました!
There was a problem hiding this comment.
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かをはっきりさせたいという感覚です。
There was a problem hiding this comment.
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は確かに全体のコード量を鑑みると冗長ですね。
There was a problem hiding this comment.
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であることは比較的明確なのかもと思いました。
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise Exception('unreachable')
のようなメッセージを付けると親切かなと思いました。
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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)になりませんか。
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。確認しておきます。
- ハッシュマップ | ||
- 追加のメモリを使用する | ||
- 足し算が可換(答えの順番が関係ない)ので1パスでOK | ||
- 引き算とかだとおそらく1パスではできない |
There was a problem hiding this comment.
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 があったかを確認すればいいですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかに両方見れば大丈夫ですね!勉強になりました。
問題へのリンク
https://leetcode.com/problems/two-sum/description/
次に解く問題
49. Group Anagrams
README.mdへ頭の中の言語化と記録をしています。