Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Written in Java Design a recursive algorithm to find the longest increasing sequence of numbers in a rectangular grid. For example, if the grid contains

Written in Java

Design a recursive algorithm to find the longest increasing sequence of numbers in a rectangular grid. For example, if the grid contains 97 47 56 36 35 57 41 13 89 36 98 75 25 45 26 17 then the longest increasing sequence of numbers is the sequence of length eight consisting of 17, 26, 36, 41, 47, 56, 57, 97. Note that there are no duplicates in the increasing sequence. With out using a input file. The code has errors and i don't know why. Need help fixing errors

Java Code import java.util.Stack; public class LongestIncreasingSequence { private static void getLongestIncreasingSequence(int[][] grid, int r, int c) { int[][] g = new int[r][c]; for (int a = 0; a < r; a++) { for (int b = 0; b < c; b++) { g[a][b] = 1; } } int maxLen = -1; int idx1 = -1; int idx2 = -1; for(int a = 0; a < r; a++){ for(int b = 0; b < c; b++){ if(a > 0 && Math.abs(grid[a][b]-grid[a-1][b])==1){ g[a][b] = Math.max(g[a][b], g[a-1][b]+1); } if(b > 0 && Math.abs(grid[a][b]-grid[a][b-1])==1){ g[a][b] = Math.max(g[a][b], g[a][b-1]+1); } if(maxLen { maxLen = g[a][b]; idx1 = a; idx2 = b; } } } display(grid,g, maxLen, idx1, idx2); } private static void display(int[][] grid, int[][] g, int maxLen, int idx1, int idx2){ Stack s1 = new Stack<>(); while(maxLen >= 1){ s1.add(grid[idx1][idx2]); if(idx1>0 && Math.abs(g[idx1-1][idx2]-g[idx1][idx2])==1){ idx1--; } else if(idx2 >0 && Math.abs(g[idx1][idx2-1]-g[idx1][idx2])==1){ idx2--; } maxLen--; } Stack s2 = helper(s1); for(Integer integer : s2){ System.out.print(integer+" "); } } public static Stack helper(Stack input){ Stack tempStack = new Stack(); while(!input.isEmpty()){ int temp = input.pop(); while (!tempStack.isEmpty() && tempStack.peek() > temp){ input push(tempStack.pop()); } tempStack.push(temp); } return tempStack; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

What is proteomics?

Answered: 1 week ago

Question

Compare and contrast the housing patterns of different cultures

Answered: 1 week ago

Question

which command is used to verify and correct file systems

Answered: 1 week ago

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago

Question

Question What are the requirements for a safe harbor 401(k) plan?30

Answered: 1 week ago