-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
interview prac/Heaps, Stacks, Queues/minimumOnStack/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Note: Write a solution with O(operations.length) complexity, since this is what you would be asked to do during a real interview. | ||
|
||
Implement a modified stack that, in addition to using push and pop operations, allows you to find the current minimum element in the stack by using a min operation. | ||
|
||
Example | ||
|
||
For operations = ["push 10", "min", "push 5", "min", "push 8", "min", "pop", "min", "pop", "min"], the output should be | ||
minimumOnStack(operations) = [10, 5, 5, 5, 10]. | ||
##### Stack과 Heap을 이용하여 문제를 풀었다. | ||
```java | ||
int[] minimumOnStack(String[] o) { | ||
Queue<Integer> q = new PriorityQueue(3); | ||
List<Integer> l = new ArrayList<>(); | ||
Stack<Integer> st = new Stack<>(); | ||
for(String s : o){ | ||
String t = s.substring(0,2); | ||
int i=0; | ||
if(t.equals("pu")){ | ||
i = Integer.valueOf(s.split(" ")[1]); | ||
st.push(i); | ||
q.add(i); | ||
}else if(t.equals("mi")){ | ||
l.add(q.peek()); | ||
}else{ | ||
i = st.pop(); | ||
q.remove(i); | ||
} | ||
} | ||
return l.stream().mapToInt(i->i).toArray(); | ||
} | ||
``` |