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)

Java: Stock span problem.(use a stack, complete the implementation of the method

.

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 prices = new ArrayList(); private ArrayList spans = new ArrayList(); public StockSpan() { super(); }

/* * 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!