THINK FIRST·CODE LATER

← All labs

Longest increasing subsequence

Problem

Given n numbers, find the longest strictly increasing subsequence (elements in their original order, not necessarily adjacent) — e.g. the longest run of improving results in a training log.

Write LongestIncreasing with dynamic programming: best[i] = length of the longest increasing subsequence that ends at a[i] = 1 + the largest best[j] over all j < i with a[j] < a[i] (take the first such j on a tie, and the first i with the largest best). Keep a previous[] array to rebuild one subsequence.

For the input

8
10 9 2 5 3 7 101 18

the program prints

best[]: [1, 1, 1, 2, 2, 3, 4, 4]
Longest increasing subsequence: 4
One of them: [2, 5, 7, 101]

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d8-6-data-1.txt

8
10 9 2 5 3 7 101 18
What your program must print for d8-6-data-1.txt
best[]: [1, 1, 1, 2, 2, 3, 4, 4]
Longest increasing subsequence: 4
One of them: [2, 5, 7, 101]

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d8-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content d8-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.