Skip to content

383. Ransom Note #17

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

Merged
merged 2 commits into from
Nov 11, 2024
Merged

383. Ransom Note #17

merged 2 commits into from
Nov 11, 2024

Conversation

colorbox
Copy link
Owner

class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int char_count[26];
Copy link

Choose a reason for hiding this comment

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

int char_count[26] = {0} のように明示的に初期化してあるほうがよりわかりやすいかと思いました。(自分はC++を書いたことがないので、書かないほうが自然だったら申し訳ありません。)

あと、HashMapと配列それぞれへのアクセスですが、Pythonだと速度比で 辞書: 配列 = 3.5: 2.9 ぐらいでした。C++での高速化の目安になるかはわかりませんが参考までに。

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

C++ ではこれは初期化されていないので、0が入っているという保証がありません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます、初期化の観点が抜けていました。

383/step1.cpp Outdated
map<char, int> character_count;
for (char c: magazine) {
if (!character_count[c]) {
character_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.

ちなみに、こっちは、アクセスした瞬間に初期化されるので不要です。

383/step1.cpp Outdated
}

for (char c: ransomNote) {
if (!character_count[c] || character_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.

https://cpprefjp.github.io/reference/map/map/op_at.html

キー x に対応する値を返す。対応する要素が存在しない場合は、要素を値初期化して参照を返す。

左側は不要です。初期化したくなければ find, count, contains を使いましょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、[]を使うと初期化されるので存在確認は不要ということですね。

Time O: O(N)

ransomに使用する文字が十分存在するかどうかをカウントすればよいと考える。
magazineにある文字を高速にチェックできないといけないので、ハッシュ(map)にして高速化を考える。

Choose a reason for hiding this comment

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

https://en.cppreference.com/w/cpp/container/map

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as Red–black trees.

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます、計算量をちゃんと理解できておりませんでした。

Copy link

Choose a reason for hiding this comment

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

そこではなくて、「ハッシュ(map)」と書いてあるけれども、C++ の map は、普通、キーがソートされているという制約から赤黒木で、ハッシュではないのです。

step1は不要な初期化やnullチェックを削除
step3は初期化を適切に行うように修正
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int char_count[26];
Copy link

Choose a reason for hiding this comment

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

= {}; で zero-initialized されます。
C++11 からですね。
https://abseil.io/tips/146

@rihib rihib mentioned this pull request Sep 9, 2024
@colorbox colorbox merged commit 37d007f into main Nov 11, 2024
@colorbox colorbox deleted the 383 branch November 11, 2024 02:32
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