Skip to content

1.Two Sum#2

Open
haniwachann wants to merge 2 commits intomasterfrom
04_HashMap
Open

1.Two Sum#2
haniwachann wants to merge 2 commits intomasterfrom
04_HashMap

Conversation

@haniwachann
Copy link
Copy Markdown
Owner

- 単純にnC2の組み合わせを全通り考える。
- ソートしてから(O(nlogn))、値iに対して、target-iとなる整数をソート済みの配列にあるか調べる(O(nlogn))方法もありそう。
- 探した要素が元々何番目の要素だったか記憶しておく必要がある。
- 2分探索が必要となる。一度自分で書いてみる。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

配列をソートしてあれば、先頭から後方に進めていくポインターと、末尾から前方に進めていくポインターを用意しておき、値の和が小さければ先頭のポインターを進める、値の和が大きければ後方のポインターを進める、を繰り返していく方針も取れると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
教えていただいた方法も、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];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

- の両隣にスペースを一つずつ空けることをおすすめいたします。

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]};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

{ の前にスペースを一つ空ける。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

Open braces should always have a space before them.

, の後ろにもスペースを一つ空けたほうがよいと思います。ただしこちらはスタイルガイドに明示的な記述を見つけられませんでした。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

丁寧にコードを見ていただきありがとうございます!
少しずつ意識せずにもこういったところをケアできるようにしていきます。

>今でなくて良いとは思いますが、余裕があればHash Tableの衝突回避のメカニズムも馴染みがなければ軽く目を通しておくと良いと思います。Open Addressing, Separete Chaining


mapは平衡2分木、問題の分類として
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実際には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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

教えていただきありがとうございます。
平衡二分探索木は、(最悪ケースの)木の高さと構造維持のための計算量の観点で、様々なアルゴリズムが考え出されているのですね...
赤黒木は回転数が減らせるので、挿入・削除の計算が償却されると0(1)なのですね...

Copy link
Copy Markdown

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/1237649827240742942/1253382452802490431

こちらも使ってよさそう。→
https://cpprefjp.github.io/reference/unordered_map/unordered_map.html

ちなみにsetは二分木
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setはmapの特殊形として実装されるのが一般的だと思うので、setもRed-Black Treeで実装されることが多いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

すみません、setがmapの特殊形というもは、mapでいうkeyが、
setのvalueで置き換えられたイメージでいいでしょうか...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうですね。木の各ノードに持たせる情報が、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++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は - 1 しないかもしれないですね。(どうせ空のループが回るだけなので。)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
範囲の端はシンプルなほうが読みやすいコードになるので、
今後はそういう観点でもコードを書けるようにしていきます。

haniwachann added a commit that referenced this pull request Nov 13, 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.

4 participants