Stack and Queues Strivers
Stack and Queues Strivers
Valid Parentheses
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
bool isValid(string s) {
stack<char>st;
for(auto i:s){
if(i=='(' || i=='{'||i=='['){
st.push(i);
}
else{
if(st.size()==0){
return false;
}
char ch=st.top();
st.pop();
if((i==')' and ch=='(') || (i=='}' and ch=='{' ) || (i==']' and
ch=='[')){
continue;
}
else{
return false;
}
}
}
return st.empty();
-----------------------------------------------------------------------------------
----------------------------------
MIN STACK
Implement the MinStack class:
Example 1:
Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output
[null,null,null,null,-3,null,0,-2]
Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
#include<algorithm>
class MinStack {
stack<pair<int,int>> st;
public:
MinStack() {
void pop() {
st.pop();
int top() {
return st.top().first;
int getMin() {
return st.top().second;
}
};
-----------------------------------------------------------------------------------
-----------------------
496. Next Greater Element I
Solved
Easy
Topics
Companies
The next greater element of some element x in an array is the first greater element
that is to the right of x in the same array.
You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is
a subset of nums2.
For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and
determine the next greater element of nums2[j] in nums2. If there is no next
greater element, then the answer for this query is -1.
Return an array ans of length nums1.length such that ans[i] is the next greater
element as described above.
Example 1:
}
break;
}}
vec.push_back(nextgreater);
}
return vec;
}
-----------------------------------------------------------------------------------
---------------------------------------
TRAPPING RAIN WATER
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array
[0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are
being trapped.
-----------------------------------------------------------------------------------
----------------------------------------------
REMOVE K DIGITS
Given string num representing a non-negative integer num, and an integer k, return
the smallest possible integer after removing k digits from num.
Example 1:
-----------------------------------------------------------------------------------
---------------------------------------------------
SLIDING WINDOW MAXIMUM
You are given an array of integers nums, there is a sliding window of size k which
is moving from the very left of the array to the very right. You can only see the k
numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
Example 1:
-----------------------------------------------------------------------------------
------------------------
The Celebrity Problem
Moderate
80/80
Average time to solve is 30m
Contributed by
189 upvotes
Asked in companies
Problem statement
There are ‘N’ people at a party. Each person has been assigned a unique id between
0 to 'N' - 1(both inclusive). A celebrity is a person who is known to everyone but
does not know anyone at the party.
Given a helper function ‘knows(A, B)’, It will returns "true" if the person having
id ‘A’ know the person having id ‘B’ in the party, "false" otherwise. Your task is
to find out the celebrity at the party. Print the id of the celebrity, if there is
no celebrity at the party then print -1.
Note:
1. The helper function ‘knows’ is already implemented for you.
2. ‘knows(A, B)’ returns "false", if A doesn't know B.
3. You should not implement helper function ‘knows’, or speculate about its
implementation.
4. You should minimize the number of calls to function ‘knows(A, B)’.
5. There are at least 2 people at the party.
6. At most one celebrity will exist.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 50
2 <= N <= 10^4
Where ‘T’ is the total number of test cases, ‘N’ is the number of people at the
party.
int findCelebrity(int n) {
// Write your code here.
int a=0;
for(int i=1;i<n;++i){
if(knows(a,i)){
a=i;
}
}
for (int i = 0; i < n; ++i) {
if (i != a && (knows(a, i) || !knows(i, a))) {
return -1; // Candidate is not a celebrity
}
}
return a;
}