Skip to content

383. Ransom Note#15

Open
ryosuketc wants to merge 1 commit intomainfrom
383_ransom_note
Open

383. Ransom Note#15
ryosuketc wants to merge 1 commit intomainfrom
383_ransom_note

Conversation

@ryosuketc
Copy link
Owner

* https://cpprefjp.github.io/lang/cpp11/initializer_lists.html
* `insert` vs. `emplace`
* `emplace` の「要素を直接つくる」とは何かと思ったが、そもそも `insert` はコンテナ外で一時オブジェクトを作ってそれをムーブしているらしい
* なので基本的には `emplace` (C++11)の方が効率が良く、そちらを使う
Copy link

Choose a reason for hiding this comment

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

Chromium と Google は、基本 insert を使えみたいですね。
https://groups.google.com/a/chromium.org/g/chromium-dev/c/7mJypsYz6AA/m/reAeaonzBQAJ
https://abseil.io/tips/112

「効率が良く」に対して、「それは何秒くらい速くなるのか、本当にディスアドバンテージはないのか」とまず感じます。「速度」はエンジニアリングをする上でかなり弱い根拠なんですね。


* 最初、「重複を許すset」みたいなデータ構造(検索や削除はO(1))があればいいのにな、と思った
* step2 で追記。`std::multiset`, `std::unordered_multiset`
* あったような気がするがぱっとわからなかったので map で実装してみた (unordered_map でもよかったが、平均計算量のよい map にしてみた。そこまで深い考察はない)。
Copy link

Choose a reason for hiding this comment

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

平均計算量は unordered_map のほうが良いですが、一部の操作については map のほうが早い、だと思います。

https://chromium.googlesource.com/chromium/src/+/HEAD/base/containers/README.md#std_unordered_map-and-std_unordered_set

In a microbenchmark on Windows, inserts of 1M integers into a std::unordered_set took 1.07x the time of std::set, and queries took 0.67x the time of std::set.

++char_count[c];
}
for (char c : ransomNote) {
if (!char_count.contains(c) || char_count[c] == 0) {
Copy link

Choose a reason for hiding this comment

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

if (--char_count[c] < 0) {
    return false;
}

のほうがシンプルだと思います。

Comment on lines +29 to +31
if (!char_count.contains(c)) {
return false;
}
Copy link

Choose a reason for hiding this comment

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

この 3 行は消せると思います。

class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
std::map<char> char_count;
Copy link

Choose a reason for hiding this comment

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

c++にあまり詳しくないのですが、こちらvalueの型は指定しなくてよいのでしょうか?
leetcode上で試してみるとcompile errorとなりました

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