Skip to content

155. Min Stack #66

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 1 commit into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ continually updating 😃.
* [717. 1-bit and 2-bit Characters](src/0717_1_bit_and_2_bit_characters/1bitand2bitc.go)
* [747. Largest Number At Least Twice of Others](./src/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)

### Stack
* [155. Min Stack](src/0155_min_stack/min_stack.go)

### String
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*
* [14. Longest Common Prefix](src/0014_longest_common_prefix/lcp.go)
Expand Down
72 changes: 72 additions & 0 deletions src/0155_min_stack/min_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
155. Min Stack
https://leetcode.com/problems/min-stack/

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
*/
// time: 2019-01-10

package minstack

// MinStack a stack that supports push, pop, top, and retrieving the minimum element in constant time.
type MinStack struct {
data []int
}

// Constructor initialize your data structure here.
func Constructor() MinStack {
return MinStack{}
}

// Push element x onto stack.
func (s *MinStack) Push(x int) {
s.data = append(s.data, x)
}

// Pop Removes the element on top of the stack.
func (s *MinStack) Pop() {
if !s.IsEmpty() {
s.data = s.data[:s.GetSize()-1]
}
}

// Top Get the top element.
func (s *MinStack) Top() int {
//if s.IsEmpty(){
// return 0
//}
// stack must have element.
return s.data[s.GetSize()-1]
}

// GetMin retrieving the minimum element in constant time.
// time complexity: O(n)
func (s *MinStack) GetMin() int {
//if s.IsEmpty() {
// return 0
//}
// stack must have element.
stackSize := s.GetSize()
ret := s.data[stackSize-1]

for i := stackSize - 2; i >= 0; i-- {
if s.data[i] < ret {
ret = s.data[i]
}
}
return ret
}

// GetSize get size of the stack.
func (s MinStack) GetSize() int {
return len(s.data)
}

func (s MinStack) IsEmpty() bool {
return s.GetSize() == 0
}
21 changes: 21 additions & 0 deletions src/0155_min_stack/min_stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package minstack

import "testing"

func TestMinStack(t *testing.T) {
obj := Constructor()
obj.Push(-2)
obj.Push(0)
obj.Push(-3)
if res := obj.GetMin(); res != -3 {
t.Errorf("expected %d, got %d", -3, res)
}

obj.Pop()
if res := obj.Top(); res != 0 {
t.Errorf("expected %d, got %d", 0, res)
}
if res := obj.GetMin(); res != -2 {
t.Errorf("expected %d, got %d", -2, res)
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
Expand Down