Skip to content
  • General
  • Programming
  • DS & Algo
  • System Design
  • Interview Questions
  • Home
  • YouTube
  • About
  • Contact
Learn to Code and Code to Learn

Learn to Code and Code to Learn

Your Journey to Code Mastery

  • General
    • Setup
  • Programming
    • C++
    • C++-11
    • c++-14
    • Python
  • DS & Algo
    • DS
    • Algo
      • Competitive Programming
        • Leetcode Problems
  • System Design
    • Design Pattern
    • SOLID Principle
  • Interview Questions
    • C++
    • Company Wise
  • Toggle search form

GFG PTOD | 24 Feb | Stock span problem | Medium level | STACK

vector<int> calculateSpan(vector<int>& arr) {
        // write code here
        stack<int> st;
        vector<int> ans(arr.size());
        for(int i=0; i<arr.size(); i++) {
            while(!st.empty() && arr[st.top()] <= arr[i])
                st.pop();
            if(st.empty()) 
                ans[i] = i+1;
            else
                ans[i] = i-st.top();
            st.push(i);
        }
        return ans;
    }
PTOD-24-Feb-2025Download

Copyright © 2025 Learn to Code and Code to Learn.

Powered by PressBook Blog WordPress theme