Question
Java: Stock span problem.(use a stack, complete the implementation of the method in the class StockSpan) . public class Main { public static void main(String[]
Java: Stock span problem.(use a stack, complete the implementation of the method in the class StockSpan)
public class Main {
public static void main(String[] args) { int prices[] = {100, 80, 60, 70, 60, 75, 85}; StockSpan stockSpan = new StockSpan(); for(int i = 0; i
}
import java.util.ArrayList;
public class StockSpan { private ArrayList
/* * Fill the spans arrayList with the span for the day or the same index in prices. * ###### Use Brute Force ###### */ void calculateSpansBruteForce() { } /* * Fill the spans arrayList with the span for the day or the same index in prices. * ###### Use better algorithm -- Stack based algorithm?! ###### */ private void calculateSpans() { } public void addPrices(int price) { prices.add(price);
}
public void print() { calculateSpansBruteForce(); for (int i = 0; i = spans.size()) { return -1; } return spans.get(day); }
}
1 A stack based efficient method to calculate stock span values // Fills array S[] with span values void calculateSpansBrute Force() spans.add(1); 1/ Calculate span value of remaining days by linearly checking // previous days for (int i = 1; i = prices.get() current Spantu; spans.add(currentSpan); ); j--)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started