Conversation
[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/)
|
お、ちょっと私の期待とは違うアルゴリズムでした。 |
nodchip
reviewed
Apr 25, 2024
| Time: O(1) | ||
| Space: O(1) | ||
| */ | ||
| class MyQueue { |
There was a problem hiding this comment.
自分が想定していたより時間計算量の悪いコードに感じました。
- push() するときは push_stack に入れる
- pop() するときは、
- pop_stack が空でない場合は pop_stack から pop() する。
- pop_stack が空の場合は、 push_stack から pop_stack にすべての要素を移動させたあと、 pop_stack から pop する。
- peek() するときは、
- pop_stack が空でない場合は pop_stack から peek() する。
- pop_stack が空の場合は、 push_stack から pop_stack にすべての要素を移動させたあと、 pop_stack から peek する。
とすると、時間計算量がすべてならしで O(1) になると思います。この書き方でも書いてみていただけますか?
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
解説を読んだのですが、誤解していました。
push時にpop_stackからpush_stackへの要素移動は不要ですね
Owner
Author
There was a problem hiding this comment.
とすると、時間計算量がすべてならしで O(1) になると思います。この書き方でも書いてみていただけますか?
レビューありがとうございました 🙏
step4.cppとしてならしでO(1)となるコードを追記しました。
おてすきでレビューお願いします
Owner
Author
コメントありがとうございます。
資料ありがとうございます、時間を見て読んでみます。 |
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.
Implement Queue using Stacks