{"id":"d5a69665-59e5-43a9-a217-139b8a16b621","name":"Online Stock Span","description":"1: Complete the next function in class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.\r\n2: The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.\r\n\r\nFor example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Calls to StockSpanner.next(int price) will have 1 &lt;= price &lt;= 10^5.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n class StockSpanner {\n public:\n stack<pair<int, int>> st;\n int time=0;\n \n StockSpanner() {\n //write your code here\n }\n \n int next(int price) {\n //write your code here\n }\n };\n\n int main() {\n StockSpanner obj;\n int price;\n while(cin >> price){\n cout<<(obj.next(price)) << endl;\n } \n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n static class StockSpanner {\r\n\r\n static class Pair{\r\n int stock;\r\n int index;\r\n }\r\n \r\n public StockSpanner() {\r\n }\r\n \r\n public int next(int price) {\r\n return -1;\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n StockSpanner obj = new StockSpanner();\r\n\r\n while(read.ready()){\r\n int price = Integer.parseInt(read.readLine());\r\n System.out.println(obj.next(price));\r\n } \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"100\r\n80\r\n60\r\n70\r\n60\r\n75\r\n85\r\n","sampleOutput":"1\r\n1\r\n1\r\n2\r\n1\r\n4\r\n6\r\n","questionVideo":"https://www.youtube.com/embed/LAGvGrd2lcE","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"05602725-5e6a-45b0-b259-8082fc25fe25","name":"Online Stock Span","slug":"online-stock-span","type":1}],"next":{"id":"017738d4-ba3f-4f1a-8c7b-c222e4bc7b84","name":"Online Stock Span Medium MCQ","type":0,"slug":"online-stock-span-medium-mcq"},"prev":{"id":"70e05d21-e5e5-49aa-9c72-19e6385277c3","name":"Minimum Remove To Make Valid Parentheses","type":3,"slug":"minimum-remove-to-make-valid-parentheses"}}}

Online Stock Span

1: Complete the next function in class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day. 2: The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price. For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].

{"id":"d5a69665-59e5-43a9-a217-139b8a16b621","name":"Online Stock Span","description":"1: Complete the next function in class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.\r\n2: The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.\r\n\r\nFor example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"Calls to StockSpanner.next(int price) will have 1 &lt;= price &lt;= 10^5.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n class StockSpanner {\n public:\n stack<pair<int, int>> st;\n int time=0;\n \n StockSpanner() {\n //write your code here\n }\n \n int next(int price) {\n //write your code here\n }\n };\n\n int main() {\n StockSpanner obj;\n int price;\n while(cin >> price){\n cout<<(obj.next(price)) << endl;\n } \n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n static class StockSpanner {\r\n\r\n static class Pair{\r\n int stock;\r\n int index;\r\n }\r\n \r\n public StockSpanner() {\r\n }\r\n \r\n public int next(int price) {\r\n return -1;\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n StockSpanner obj = new StockSpanner();\r\n\r\n while(read.ready()){\r\n int price = Integer.parseInt(read.readLine());\r\n System.out.println(obj.next(price));\r\n } \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"100\r\n80\r\n60\r\n70\r\n60\r\n75\r\n85\r\n","sampleOutput":"1\r\n1\r\n1\r\n2\r\n1\r\n4\r\n6\r\n","questionVideo":"https://www.youtube.com/embed/LAGvGrd2lcE","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"05602725-5e6a-45b0-b259-8082fc25fe25","name":"Online Stock Span","slug":"online-stock-span","type":1}],"next":{"id":"017738d4-ba3f-4f1a-8c7b-c222e4bc7b84","name":"Online Stock Span Medium MCQ","type":0,"slug":"online-stock-span-medium-mcq"},"prev":{"id":"70e05d21-e5e5-49aa-9c72-19e6385277c3","name":"Minimum Remove To Make Valid Parentheses","type":3,"slug":"minimum-remove-to-make-valid-parentheses"}}}
plane

Editor


Loading...

Online Stock Span

medium

1: Complete the next function in class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day. 2: The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price. For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].

Constraints

Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

100 80 60 70 60 75 85

Sample Output

1 1 1 2 1 4 6

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode