155. Min Stack - #101
Open
kitano-kazuki wants to merge 1 commit into
Open
Conversation
oda
reviewed
Jun 8, 2026
|
|
||
| def push(self, value: int) -> None: | ||
| min_value = self.stack[-1].min_value if self.stack else float("inf") | ||
| self.stack.append(MinStackElement(value, min(min_value, value))) |
There was a problem hiding this comment.
これ、まあいいですが、ちょっとテクニカルですね。
current_min = value
if self.stack and self.stack[-1].min_value < current_min:
current_min = self.stack[-1].min_value
self.stack.append(MinStackElement(value, min_value))if self.stack:
current_min = min(self.stack[-1].min_value, value)
else:
current_min = valuecurrent_min = min([x.min_value for x in self.stack[-1:]] + [value])まあ、こういうのもありますが。
Collaborator
Author
There was a problem hiding this comment.
1行にif文含めるのはたしかにちょっと認知負荷あがりそうですね
current_min = min([x.min_value for x in self.stack[-1:]] + [value])
この書き方は初めてみました。もしself.stack = []ならself.stack[-1:]も[]になるので, min([value])になるんですね。なるほど
There was a problem hiding this comment.
Ruby だと safe navigation operator で
current_min = [value, stack.last&.min_value].compact.minと書けたりします。
const currentMin = Math.min(value, ...this.stack.slice(-1).map(el => el.minValue));
const currentMin = Math.min(value, this.stack.at(-1)?.minValue ?? Infinity);うーん。まあ、難しいですね。愚直に書くのが一番です。
Collaborator
Author
There was a problem hiding this comment.
そうですね、愚直な方法が良さそうです
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.
https://leetcode.com/problems/min-stack/description/