Longest Increasing Subsequence on a Grid Path
Time limit1sMemory limit1024 MB
Find the longest increasing subsequence possible among all shortest top-left to bottom-right paths in an N by N grid of integers.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
Jeonghwi wants to move from the top-left cell to the bottom-right cell of an array of integers along a shortest path. He can only move between cells that share an edge.
While moving, Jeonghwi collects the integers he passes, in order, and gives them to Seonghyeon as a gift. Seonghyeon likes sequences with a long increasing subsequence, so Jeonghwi wants to build a sequence whose longest increasing subsequence is as long as possible.
Jeonghwi has no time to find the path because he is writing a problem. Given the array, find the maximum possible length of the longest increasing subsequence among all sequences he can build.
Input
The first line contains ().
Each of the next lines contains integers separated by spaces. Each integer is between and , inclusive.
Output
Print the maximum possible length of the longest increasing subsequence.
Hints
- Python users are recommended to submit with PyPy.
- A subsequence is a sequence obtained by picking one or more elements from the given sequence and listing them in their original order.
- An increasing subsequence is a sequence in which every element except the first is greater than the element right before it. In other words, for a subsequence of length , if holds for every , then is an increasing subsequence.
- The longest increasing subsequence is the increasing subsequence of a given sequence that has the greatest length.