Typing Class Progress
A night-school typing class records each student's speed, in words per minute, after every session. Speeds go up and down. At the end of term the teacher's progress note quotes some sessions, in date order, each strictly faster than the one quoted before it.
She wants the longest such chain for each student.
Your task
Write longestImprovement(speeds). Return the length of the longest list of sessions you can pick, in their original order, where each picked speed is strictly greater than the previous one. Picked sessions need not be next to each other.
An empty record gives 0. Equal speeds do not count as an improvement.
longestImprovement(speeds: int[]) → int
Examples
speeds = [42, 38, 45, 40, 47, 41, 43, 50]
5
The best chain is 38, 40, 41, 43, 50, which has length 5. Starting with the first session, 42, gives at most 42, 45, 47, 50, which has length 4, so the best chain starts with a dip.
speeds = [30, 30, 30]
1
Every session is the same speed. A chain needs each speed to be strictly faster, so the best is one session on its own.
Constraints
- 0 ≤ speeds.length ≤ 2,500
- 0 ≤ speeds[i] ≤ 200
Start with brute force
Try every subset of sessions and keep the longest strictly increasing one. With 2,500 sessions there are 2²⁵⁰⁰ subsets; even 40 sessions gives about a trillion.
Two shortcuts are wrong. The longest run of consecutive rising sessions misses chains that skip bad days. Greedily taking every session that beats the last one taken fails on [1, 9, 2, 3, 4]: it takes 1, 9 and gets stuck.
Hints
0 of 4 shown- Hint 1
Take the session with speed 43 in the first example. Suppose the chain must end at 43. Which earlier sessions could come just before it?
- Hint 2
Any earlier, slower session: 42, 38, 40 or 41. The best chain ending at 43 is one longer than the best chain ending at any of those. So track, for every session, the best chain that ends exactly there.
- Hint 3
Let
best[i]be the length of the longest improving chain that ends at sessioni. Every session alone is a chain of 1. Thenbest[i] = 1 + max(best[j])over earlierjwithspeeds[j] < speeds[i]. - Hint 4
Fill
bestleft to right with a double loop: for eachi, scan everyj < i. The answer is the largest value anywhere inbest, not the last one, since a chain can end at any session.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Fix a chain's last session and the question gets small: the best chain ending at i is session i added to the best chain ending at some earlier, slower session. Compute that for every i, left to right.
Trace [42, 38, 45, 40, 47, 41, 43, 50]:
- Session 42 has nothing before it, so best = 1.
- Session 38 has no slower session before it, because 42 is faster, so best = 1.
- Session 45 can follow 42 or 38, which both have chains of length 1, so best = 2.
- Session 40 can follow only 38, so best = 2.
- Session 47 can follow any earlier session, and the longest chain among them is 2 (ending at 45 or 40), so best = 3.
- Session 41 can follow 38 (chain length 1) or 40 (chain length 2), so best = 3.
- Session 43 can follow 42, 38, 40 or 41, and the longest chain among them is the one ending at 41, with length 3, so best = 4.
- Session 50 can follow every earlier session, and the longest chain among them is the one ending at 43, with length 4, so best = 5.
The answer is the largest value, 5. With 2,500 sessions the double loop does about 3 million comparisons, which is quick. A binary-search method gets O(n log n); see this lesson.
function longestImprovement(speeds) {
const n = speeds.length;
const best = new Array(n).fill(1); // best[i] = longest chain ending at session i
let answer = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++) {
// session j can come right before session i if it was slower
if (speeds[j] < speeds[i] && best[j] + 1 > best[i]) best[i] = best[j] + 1;
}
answer = Math.max(answer, best[i]);
}
return answer;
}def longest_improvement(speeds):
n = len(speeds)
best = [1] * n # best[i] = longest chain ending at session i
for i in range(n):
for j in range(i):
# session j can come right before session i if it was slower
if speeds[j] < speeds[i] and best[j] + 1 > best[i]:
best[i] = best[j] + 1
return max(best, default=0)Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.