Question: PLEASE Modify the addCol(j) and removeCol(j) operations only so it runs O(row() + col()-j) instead of O(row() *(col() -j)) time. import java.util.List; import java.util.ArrayList; import

PLEASE Modify the addCol(j) and removeCol(j) operations only so it runs O(row() + col()-j) instead of O(row() *(col() -j)) time. 

PLEASE Modify the addCol(j) and removeCol(j) operations only so it runs O(row()

+ col()-j) instead of O(row() *(col() -j)) time. import java.util.List; import java.util.ArrayList; import java.util.List; import java.util.ArrayList; import java.util.Random; import java.util.Collections;  public class FasterTable implements Table { List> tab; int nrows, ncols; public FasterTable(Class t) { nrows = 0; ncols = 0; tab = new ArrayList>(); } public int rows() { return nrows; } public int cols() { return ncols; } public T get(int i, int j) { if (i  rows() - 1 || j  cols()-1) throw new IndexOutOfBoundsException(); return tab.get(i).get(j); } public T set(int i, int j, T x) { if (i  rows() - 1 || j  cols()-1) throw new IndexOutOfBoundsException(); return tab.get(i).set(j, x); } public void addRow(int i) { if (i  rows()) throw new IndexOutOfBoundsException(); nrows++; List row = new ArrayList(); for (int j = 0; j null); tab.add(i, row); } public void removeRow(int i) { if (i  rows() - 1) throw new IndexOutOfBoundsException(); nrows--; tab.remove(i); } public void addCol(int j) { // this method is too slow!  if (j  cols()) throw new IndexOutOfBoundsException(); ncols++; // this loop takes O( rows*(cols()-j) ) time  for (int i = 0; i null); // O( cols()-j ) time  } public void removeCol(int j) { // this method is too slow!  if (j  cols() - 1) throw new IndexOutOfBoundsException(); ncols--; // this loop takes O( rows*(cols()-j) ) time  for (int i = 0; i // O( cols()-j ) time  } public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i for (int j = 0; j " "); } sb.append(" "); } return sb.toString(); } public static void main(String[] args) { Tester.testTable(new FasterTable(Integer.class)); } } 

Hint 1: To make addCol(i) fast enough, you have to avoid calling add(i,nul1) for each row in your table. You can, however, afford to call add (null) for each each row. Hint 2: To make removeCol(i) fast enough, you have to avoid calling remove(i) for each row in your table. You can, however, afford to call set(i,null) for each row

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!