Min Stack - Leetcode 155 - Stacks (Python)

  Рет қаралды 5,040

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 20
@GregHogg
@GregHogg 5 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@ceciljoel9577
@ceciljoel9577 4 ай бұрын
why we can just maintain record of a min value ? why min_stk?
@ChristopherJones-ke8yg
@ChristopherJones-ke8yg 3 ай бұрын
Perhaps because if you pop the min value then you will have to update min_value using something like min(self.stack), since youre not using min_stk?
@ceciljoel9577
@ceciljoel9577 3 ай бұрын
@@ChristopherJones-ke8yg oh nice insight! Thanks
@ThoXuanLe
@ThoXuanLe 4 ай бұрын
Thank you Greg Hogg!!
@JoeTan-nq4fq
@JoeTan-nq4fq 2 ай бұрын
For each node in the stack, store the current and min value thus far. In this way, we dun need the min_stack. def push(self, val: int) -> None: # Each node stores the val and min val thus far if not self.stack: self.stack.append((val, val)) else: minn = min(self.stack[-1][1], val) self.stack.append((val, minn))
@ParkavanCK
@ParkavanCK 4 ай бұрын
Thank you Greg Hogg!!!!!!
@unanimed1
@unanimed1 2 ай бұрын
I used a list of tuples for implementing the class, the first tuple item is to track the added elements and the second tuple item is to track the min values, is it a valid solution? public class MinStack { private List list; private int minNumber; public MinStack() { list = new List(); minNumber = int.MaxValue; } public void Push(int val) { list.Add(Tuple.Create(val, minNumber)); if(val < minNumber) { minNumber = val; } } public void Pop() { minNumber = list.ElementAt(list.Count - 1).Item2; list.RemoveAt(list.Count - 1); } public int Top() { return list.ElementAt(list.Count - 1).Item1; } public int GetMin() { return minNumber; } }
@kdiffin
@kdiffin Ай бұрын
Thanks for this
@amjadalthabteh4680
@amjadalthabteh4680 4 ай бұрын
I thought that we appended the first val into the stk, hence [-1], i am confused on what value we are comparing it too, or are just returning the same val, just confused on the PUSH function lol, anyways thanks for all the help, youre truly amazing Greg!!!!!!
@amjadalthabteh4680
@amjadalthabteh4680 4 ай бұрын
The min_stk sorry, i though we appened into the min stk
@Infinitely16
@Infinitely16 4 ай бұрын
There are 3 conditions. First, if the min_stk is empty, then the current val is the minimum, so just append it. Second, if the current val we are looking at is greater than the current minimum value, then we just append the current minimum value, since the new one isn't smaller than it. Third, if the new val is less than the current min, then we append this new val as the new minimum.
@JACK-of5vi
@JACK-of5vi 10 ай бұрын
hi Greg, thank you for sharing this video! I've got a question: as a beginner in DSA and leetcode, instead of learning all the DSA topics in one big chunk, can I learn one topic of DSA, and then go ahead to practice the corresponding questions for that topic? And repeat the same process for other topics?
@JACK-of5vi
@JACK-of5vi 10 ай бұрын
for example, learn about topic like array, then practice array-related questions. then move on to another topic such as tree, and then repeat the process?
@xingyuxiang1637
@xingyuxiang1637 10 ай бұрын
@f5vi Code Signal may be better. But the hassles of grinding the problems will not be too much different. One has to type through the indexes to solve Sudoku. Let the hard questions and brain teasers be. There are hard questions, which imply a lot of easy and medium questions practices.
@GregHogg
@GregHogg 10 ай бұрын
You can certainly break it down into similar categories of problems, yes :)
@mohamedmohsen-rc3jf
@mohamedmohsen-rc3jf Ай бұрын
mmmm i think there is a better solution , you can have an amoritized run O(1) beside space of O(1) as well , by having let's say a tuple that has the min val as the first element and the freq of occurance as a second element and when we push we just see if the new getted value is less than the min we have or not if it is we update our min tuple with the new one and freq = 1 , if it is equal we increase the freq and when we pop any element we just check if it is the min element we store or not . if it is decrease the freq , if not it is ok. if the freq = 0 just iterate again on the stack and get the new min then iterate again to find the freq of occurance . i think it is a valid and better solution
@xingyuxiang1637
@xingyuxiang1637 10 ай бұрын
To be more API-friendly: from collections import deque self.stack = deque() return min(self.stack) Hackerrank will need import. So, writing out the import statement is useful.
@GregHogg
@GregHogg 10 ай бұрын
But you don't need a queue, it's just a stack
@moonlight-td8ed
@moonlight-td8ed 5 ай бұрын
thats an o(n)
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 382 М.
Design Hashmap - Leetcode 706 - Python
14:30
NeetCodeIO
Рет қаралды 39 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Build Everything with AI Agents: Here's How
39:58
David Ondrej
Рет қаралды 26 М.
Largest Rectangle in Histogram - Leetcode 84 - Stacks (Python)
10:59
Learn Machine Learning Like a GENIUS and Not Waste Time
15:03
Infinite Codes
Рет қаралды 370 М.
House Robber - Leetcode 198 - Dynamic Programming (Python)
13:15
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 170 М.
Daily Temperatures - Leetcode 739 - Stacks (Python)
12:35
Greg Hogg
Рет қаралды 9 М.
Koko Eating Bananas - Leetcode 875 - Binary Search (Python)
13:34
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 781 М.
Starting a Career in Data Science (10 Thing I Wish I Knew…)
10:42
Sundas Khalid
Рет қаралды 260 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.