-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】11問目 895. Maximum Frequency Stack #70
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # 回数ごとに違うstackを作る | ||
| class FreqStack: | ||
|
|
||
| def __init__(self): | ||
| self.freq = defaultdict(int) | ||
| self.stack = [[]] | ||
| self.max_freq = 0 | ||
|
|
||
| def push(self, val: int) -> None: | ||
| self.freq[val] += 1 | ||
| if len(self.stack) < self.freq[val]: | ||
| self.stack.append([]) | ||
| self.max_freq = max(self.max_freq, self.freq[val]) | ||
| self.stack[self.freq[val] - 1].append(val) | ||
|
Comment on lines
+9
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L.14 で
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. おっしゃるとおり、 |
||
|
|
||
| def pop(self) -> int: | ||
| val = self.stack[self.max_freq - 1].pop() | ||
| self.freq[val] -= 1 | ||
| if not self.stack[self.max_freq - 1]: | ||
| self.max_freq -= 1 | ||
| return val | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # stacksをdefaultdict(list)を使うことで空の場合の確認を不要にした。 | ||
| class FreqStack: | ||
|
|
||
| def __init__(self): | ||
| self.frequency = defaultdict(int) | ||
| self.stacks = defaultdict(list) | ||
| self.max_frequency = 0 | ||
|
|
||
| def push(self, val: int) -> None: | ||
| self.frequency[val] += 1 | ||
| self.max_frequency = max(self.max_frequency, self.frequency[val]) | ||
| self.stacks[self.frequency[val]].append(val) | ||
|
|
||
| def pop(self) -> int: | ||
| if not self.stacks[self.max_frequency]: | ||
| return None | ||
| top_value = self.stacks[self.max_frequency].pop() | ||
| self.frequency[val] -= 1 | ||
| while self.max_frequency != 0 and not self.stacks[self.max_frequency]: | ||
| self.max_frequency -= 1 | ||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この部分は while 文でなく if 文でも十分だったりしませんかね?( また条件としては後者のみで十分なように思いました。前者が false となるのは There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 追加で L.15 の validation ももしかしたら不要かも?と思ってきました。(上で書いた
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
そのことをコードの読み手が考えるのは辛いかなと思いましたが、自明かもしれませんね。 空に対するpopの例外処理についてのコメントもありがとうございます。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、コードを読んだときに while を使っている理由がなにかあるんじゃないかと考えてしまったので、不要な付加情報があるとむしろ読みづらい(または誤解を招きやすい)んじゃないかと思いました。 例外処理に関してはあくまで僕個人の意見なので、他の方の意見も気になるところです。 |
||
| return top_value | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class FreqStack: | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここ普通開けない気がします。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pep8とgoogleのコーディング規約を再度確認しました。 |
||
| def __init__(self): | ||
| self.frequency = defaultdict(int) | ||
| self.stacks = defaultdict(list) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stacks は、下から整数が入っているはずなので、これ、リストでもいいですかね。ちょっと書くのが面倒になりますか。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、step 1 はそうしたんですね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. むしろ最初はlistしか思いつかず、リファクタリングしててdefaultdictに気づきました。 |
||
| self.max_frequency = 0 | ||
|
|
||
| def push(self, val: int) -> None: | ||
| self.frequency[val] += 1 | ||
| self.stacks[self.frequency[val]].append(val) | ||
| self.max_frequency = max(self.max_frequency, self.frequency[val]) | ||
|
|
||
| def pop(self) -> int: | ||
| value = self.stacks[self.max_frequency].pop() | ||
| self.frequency[value] -= 1 | ||
| while self.max_frequency != 0 and not self.stacks[self.max_frequency]: | ||
| self.max_frequency -= 1 | ||
| return value | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビュー前に自分で解いてみたときには
self.max_freqを持たせるという発想がなかったので、なるほどと思いました。(ただのコメントです。)(自分で解いたときは Step1 と同様に list で扱っていて、
self.stackの長さをコントロールすることで list の右端がここでいうself.max_freqに対応するようにしていて、常にself.stack[-1]を参照するようにしていました)