-
Notifications
You must be signed in to change notification settings - Fork 0
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?
Conversation
for i in range(len(nums))と for i, _ in enumerate(nums) はどっちが早いんだろう | ||
https://stackoverflow.com/questions/11990105/rangelenlist-or-enumeratelist | ||
rangeの方がちょい早いらしい?enumerateはtupleを出力してるからか。 | ||
rangeの方が絶対にわかりやすくはある。 |
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.
Python がそもそもあんまり速くないので、このあたりの速度はあまり気にしなくていいでしょう。
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.
より正確に書くと、計算量を求め、データサイズを代入し、おおよその計算ステップ数を求め、言語ごとの処理速度を考慮し、許容される実行時間内に実行可能な計算ステップ数以内に十分収まっていれば、ひとまず大丈夫だと思います。速度を求めるなら、言語を変えることも一つの手だと思います。
https://github.com/t0hsumi/leetcode/pull/11 | ||
|
||
入力をソートして両端から調べていく方法もある | ||
left、rightが行きすぎて戻ることがあるからHashmapのものより少しだけ遅そうに見える |
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.
いろんなパターンの実装をされていて素晴らしいと思いました。
left、rightが行きすぎて戻ることがある
僕の理解のために教えて欲しいのですが、「行きすぎる」ってどういうパターンでしょうか。
コードを見ると、whileループの中で、
- leftは0から増えるだけ
- rightは最大値から減るだけ
に見えて、あれ?っとなりました。
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, b, c, d, e]とかで target = b + d の時 a + e < target < c + e だと c まで行きすぎてから戻るのかなとなんとなく思ってたのですが、 b + e > b + d が必ず成立するのでそんなことにはならなかったです。
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_index_pairs = [] |
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.
パフォーマンスの違いはほとんどないと思いますが、sorted 関数の key 引数を利用すれば、リストを新たに生成せずとも処理が可能です
num_index_pairs = sorted(enumerate(nums), key=lambda x: x[1])
|
||
C++も読んだことない | ||
解法は同じ | ||
赤黒木 std::mapというものも使えるらしい |
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.
C++ の企画上は、 std::map を赤黒木で実装しなければならないとは書いておらず、計算量のみが規定されています。ただし、赤黒木で実装されることが多いようです。
https://timsong-cpp.github.io/cppwp/n4950/map.access
Complexity: Logarithmic.
https://en.cppreference.com/w/cpp/container/map
Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as Red–black trees.
for i in range(len(nums))と for i, _ in enumerate(nums) はどっちが早いんだろう | ||
https://stackoverflow.com/questions/11990105/rangelenlist-or-enumeratelist | ||
rangeの方がちょい早いらしい?enumerateはtupleを出力してるからか。 | ||
rangeの方が絶対にわかりやすくはある。 |
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/