class StockSpanner { public: int index; stack st; StockSpanner() { index =-1; // st.clear(); } int next(int price) { index+=1; int ans; while(!st.empty() && st.top().first
What a timing. I was solving this problem from SDE sheet and at first got disappointed as there was no link to this problem. I'm glad that I decided to checkout the channel. Unbeatable explanation like always. Thanks.
I solved it by building a monotonic stack (executed in lesser time when compared to above optimal approach). It involves storing "Stack" type custom class which has "val" and "span" in Deque. The approach involves storing the Stack type classes in decreasing fashion. Whenever we encounter the value greater than stack.peek(), pop the elements and add it's corresponding span values. Below is my code for understanding. class StockSpanner { Deque stack; public StockSpanner() { stack = new ArrayDeque(); } public int next(int price) { int span = 1; while (!stack.isEmpty() && stack.peek().val
Java Code class StockSpanner { Stack st = new Stack(); int ind; public StockSpanner() { st = new Stack(); ind = -1; } public int next(int price) { ind++; while(!st.isEmpty() && st.peek()[1]
Arre love you bhaiya ❤ i have done arrays, linked list, binary search, recursion ….. and was looking for stack n queue. What a timing bhaiya ji 🥹 mauj kara di
won't space complexity also be O(2N) as we are storing both value and index in stack? Also I tried to do the question by myself, and here is my solution: class StockSpanner { public: vector prices; stack st; StockSpanner() { index = -1; }
int next(int price) { int ans; prices.push_back(price); while(!st.empty() && price >= prices[st.top()]) st.pop(); if(!st.empty()) ans = prices.size()-1-st.top(); else ans = prices.size(); st.push(prices.size()-1); return ans; } };
Java Code class Pair { I index; V value; public Pair(I key, V value) { this.index = key; this.value = value; } public I getIndex() { return index; } public V getValue() { return value; } } public class StockSpanner {
Stack st; int index; public StockSpanner() { this.st=new Stack(); this.index=-1; } public int next(int price) { index++; while(!st.empty()&&st.peek().value
I was able to come up with solution by myself.
All thanks to you for making me improve my critical thinking
class StockSpanner {
public:
int index;
stack st;
StockSpanner() {
index =-1;
// st.clear();
}
int next(int price) {
index+=1;
int ans;
while(!st.empty() && st.top().first
What a timing. I was solving this problem from SDE sheet and at first got disappointed as there was no link to this problem. I'm glad that I decided to checkout the channel. Unbeatable explanation like always. Thanks.
I solved it by building a monotonic stack (executed in lesser time when compared to above optimal approach). It involves storing "Stack" type custom class which has "val" and "span" in Deque. The approach involves storing the Stack type classes in decreasing fashion. Whenever we encounter the value greater than stack.peek(), pop the elements and add it's corresponding span values. Below is my code for understanding.
class StockSpanner {
Deque stack;
public StockSpanner() {
stack = new ArrayDeque();
}
public int next(int price) {
int span = 1;
while (!stack.isEmpty() && stack.peek().val
Java Code
class StockSpanner {
Stack st = new Stack();
int ind;
public StockSpanner() {
st = new Stack();
ind = -1;
}
public int next(int price) {
ind++;
while(!st.isEmpty() && st.peek()[1]
liked it better when u gave the clear code at the end :)
Brilliant explanation , Thank you
class StockSpanner {
Stackst;
public StockSpanner(){
st=new Stack();
}
public int next(int price){
int spam=1;
while(!st.isEmpty()&&st.peek ()[0]
Arre love you bhaiya ❤ i have done arrays, linked list, binary search, recursion ….. and was looking for stack n queue.
What a timing bhaiya ji 🥹 mauj kara di
Great solution!
in 2nd approach space complexity should be O(2n) right ? (because we are using a stack of )
Able to solve Thanks :)
won't space complexity also be O(2N) as we are storing both value and index in stack? Also I tried to do the question by myself, and here is my solution:
class StockSpanner {
public:
vector prices;
stack st;
StockSpanner() {
index = -1;
}
int next(int price) {
int ans;
prices.push_back(price);
while(!st.empty() && price >= prices[st.top()])
st.pop();
if(!st.empty()) ans = prices.size()-1-st.top();
else ans = prices.size();
st.push(prices.size()-1);
return ans;
}
};
You are also using prices vector and stack here so space complexity will be O(2N) here also.
why do we need to reinitialize the index to -1 in the stockspanner() method guys? if we have declared it globally?
thanks bhaiya
Understood
thanks
suppose my input is
10 10 10 10
Sample Output 1:
1 1 1 1
then how can you handle it
I think sample output is wrong coz in condition it is stated that it will count for price less than or equal to current days.
understood
Java Code
class Pair {
I index;
V value;
public Pair(I key, V value) {
this.index = key;
this.value = value;
}
public I getIndex() {
return index;
}
public V getValue() {
return value;
}
}
public class StockSpanner {
Stack st;
int index;
public StockSpanner() {
this.st=new Stack();
this.index=-1;
}
public int next(int price) {
index++;
while(!st.empty()&&st.peek().value
First view and comment
😁
1st comment
Please pin me
for 2 days❤
I'm waiting this for years ...thanks striver @takeUforward.... yesterday u said and u delivered
Thanks