Conversation
| - 単純にnC2の組み合わせを全通り考える。 | ||
| - ソートしてから(O(nlogn))、値iに対して、target-iとなる整数をソート済みの配列にあるか調べる(O(nlogn))方法もありそう。 | ||
| - 探した要素が元々何番目の要素だったか記憶しておく必要がある。 | ||
| - 2分探索が必要となる。一度自分で書いてみる。 |
There was a problem hiding this comment.
配列をソートしてあれば、先頭から後方に進めていくポインターと、末尾から前方に進めていくポインターを用意しておき、値の和が小さければ先頭のポインターを進める、値の和が大きければ後方のポインターを進める、を繰り返していく方針も取れると思います。
There was a problem hiding this comment.
ありがとうございます!
教えていただいた方法も、2文探索と同じ計算量(logn)で探せて、
解が配列の中で偶然端の方にあると早く探せる感じですね。
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| map<int, int> num_to_indexes = {}; | ||
| for (int i = 0; i < size(nums); i++) { | ||
| int diffrence = target-nums[i]; |
There was a problem hiding this comment.
- の両隣にスペースを一つずつ空けることをおすすめいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
Other binary operators usually have spaces around them
| for (int i = 0; i < size(nums); i++) { | ||
| int diffrence = target-nums[i]; | ||
| if (num_to_indexes.contains(diffrence)) { | ||
| return{i,num_to_indexes[diffrence]}; |
There was a problem hiding this comment.
{ の前にスペースを一つ空ける。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
Open braces should always have a space before them.
, の後ろにもスペースを一つ空けたほうがよいと思います。ただしこちらはスタイルガイドに明示的な記述を見つけられませんでした。
There was a problem hiding this comment.
丁寧にコードを見ていただきありがとうございます!
少しずつ意識せずにもこういったところをケアできるようにしていきます。
| >今でなくて良いとは思いますが、余裕があればHash Tableの衝突回避のメカニズムも馴染みがなければ軽く目を通しておくと良いと思います。Open Addressing, Separete Chaining | ||
|
|
||
|
|
||
| mapは平衡2分木、問題の分類として |
There was a problem hiding this comment.
実際にはRed-Black Treeで実装されることが一般的だそうです。self-balanced binary search treeとしては、他にAVL木などもありますね。self-balancedということで、binary search treeの偏りの問題に対して、回転という操作を加えることでworst caseの深さがO(n)にならないようにするデータ構造です。
https://stackoverflow.com/questions/2558153/what-is-the-underlying-data-structure-of-a-stl-set-in-c
https://en.wikipedia.org/wiki/AVL_tree
There was a problem hiding this comment.
教えていただきありがとうございます。
平衡二分探索木は、(最悪ケースの)木の高さと構造維持のための計算量の観点で、様々なアルゴリズムが考え出されているのですね...
赤黒木は回転数が減らせるので、挿入・削除の計算が償却されると0(1)なのですね...
There was a problem hiding this comment.
平衡二分木は一つでいいので聞かれたときに、こういう感じのやつと答えられるようにしておくといいと思います。
以前、私が吐き出してみたときの記述です。
https://discord.com/channels/1084280443945353267/1237649827240742942/1253382452802490431
| こちらも使ってよさそう。→ | ||
| https://cpprefjp.github.io/reference/unordered_map/unordered_map.html | ||
|
|
||
| ちなみにsetは二分木 |
There was a problem hiding this comment.
setはmapの特殊形として実装されるのが一般的だと思うので、setもRed-Black Treeで実装されることが多いと思います。
There was a problem hiding this comment.
すみません、setがmapの特殊形というもは、mapでいうkeyが、
setのvalueで置き換えられたイメージでいいでしょうか...
There was a problem hiding this comment.
そうですね。木の各ノードに持たせる情報が、mapだとkey,valueのペアをもたせますが、setだとvalue(あるいはkey)のみをもたせるだけの違いですね。
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| for (int i = 0; i < nums.size() - 1; i++) { |
There was a problem hiding this comment.
ありがとうございます。
範囲の端はシンプルなほうが読みやすいコードになるので、
今後はそういう観点でもコードを書けるようにしていきます。
<問題>
https://leetcode.com/problems/two-sum/description/