Challenge: Longest Increasing Run (Not Neighbours)
Completed
Read a list of numbers. Print the length of the longest strictly increasing subsequence – numbers picked from left to right, not necessarily next to each other. For 10 9 2 5 3 7 101 18 the answer is 4 (for example 2 3 7 18).
Input
One line of whole numbers (at most 1000).
Output
One number.
Example
Input
10 9 2 5 3 7 101 18
Output
4
Editor
Python
Hints
best[i] = length of the longest increasing subsequence that ENDS at position i.
best[i] = 1 + max(best[j]) for every j < i with a[j] < a[i].
Solution explanation
O(n²) DP. (With binary search it can be done in O(n log n) – a nice challenge for later!)
Show solution
a = list(map(int, input().split()))
best = [1] * len(a)
for i in range(len(a)):
for j in range(i):
if a[j] < a[i] and best[j] + 1 > best[i]:
best[i] = best[j] + 1
print(max(best)) Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module