Open
Conversation
oda
reviewed
Oct 1, 2025
| * https://cpprefjp.github.io/lang/cpp11/initializer_lists.html | ||
| * `insert` vs. `emplace` | ||
| * `emplace` の「要素を直接つくる」とは何かと思ったが、そもそも `insert` はコンテナ外で一時オブジェクトを作ってそれをムーブしているらしい | ||
| * なので基本的には `emplace` (C++11)の方が効率が良く、そちらを使う |
There was a problem hiding this comment.
Chromium と Google は、基本 insert を使えみたいですね。
https://groups.google.com/a/chromium.org/g/chromium-dev/c/7mJypsYz6AA/m/reAeaonzBQAJ
https://abseil.io/tips/112
「効率が良く」に対して、「それは何秒くらい速くなるのか、本当にディスアドバンテージはないのか」とまず感じます。「速度」はエンジニアリングをする上でかなり弱い根拠なんですね。
nodchip
reviewed
Oct 1, 2025
|
|
||
| * 最初、「重複を許すset」みたいなデータ構造(検索や削除はO(1))があればいいのにな、と思った | ||
| * step2 で追記。`std::multiset`, `std::unordered_multiset` | ||
| * あったような気がするがぱっとわからなかったので map で実装してみた (unordered_map でもよかったが、平均計算量のよい map にしてみた。そこまで深い考察はない)。 |
There was a problem hiding this comment.
平均計算量は unordered_map のほうが良いですが、一部の操作については map のほうが早い、だと思います。
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) { |
There was a problem hiding this comment.
if (--char_count[c] < 0) {
return false;
}のほうがシンプルだと思います。
Comment on lines
+29
to
+31
| if (!char_count.contains(c)) { | ||
| return false; | ||
| } |
nanae772
reviewed
Oct 2, 2025
| class Solution { | ||
| public: | ||
| bool canConstruct(string ransomNote, string magazine) { | ||
| std::map<char> char_count; |
There was a problem hiding this comment.
c++にあまり詳しくないのですが、こちらvalueの型は指定しなくてよいのでしょうか?
leetcode上で試してみるとcompile errorとなりました
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
383. Ransom Note
https://leetcode.com/problems/ransom-note/