The maximum value in lis is 4, which is the length of the longest increasing subsequence.
Final Answer:
4 -> Option A
Quick Check:
LIS length = 4 [OK]
Hint: Trace LIS array updates carefully for each element [OK]
Common Mistakes:
Counting non-increasing sequences
Forgetting to update lis[i] with max value
Misreading the nested loops
4. Identify the bug in this Rust function that attempts to compute the LIS length:
fn length_of_lis(nums: Vec<i32>) -> usize {
let n = nums.len();
let mut lis = vec![1; n];
for i in 0..n {
for j in 0..i {
if nums[i] < nums[j] {
lis[i] = lis[i].max(lis[j] + 1);
}
}
}
*lis.iter().max().unwrap()
}
What is the error?
medium
A. The function should return the sum of lis instead of max
B. The vector lis should be initialized with zeros instead of ones
C. The outer loop should start from 1 instead of 0
D. The comparison should be nums[i] > nums[j] instead of nums[i] < nums[j]
Solution
Step 1: Analyze the comparison condition
The LIS requires increasing order, so we must check if nums[i] > nums[j]. The code incorrectly uses nums[i] < nums[j], which finds decreasing sequences.
Step 2: Confirm other parts are correct
Initialization with ones and returning max are correct. Starting outer loop at 0 is valid but less efficient.
Final Answer:
The comparison should be nums[i] > nums[j] instead of nums[i] < nums[j] -> Option D
Quick Check:
Correct comparison is > for LIS [OK]
Hint: Check comparison signs for increasing sequences [OK]
Common Mistakes:
Using less than instead of greater than
Initializing lis with zeros
Returning sum instead of max
5. Given a list nums = vec![3, 10, 2, 1, 20], how would you modify the LIS algorithm to also return the actual longest increasing subsequence, not just its length?
hard
A. Sort the list first, then find LIS length on sorted list
B. Use a stack to push elements and pop when sequence breaks
C. Keep a predecessor array to track previous indices and reconstruct the sequence after computing LIS lengths
D. Return the maximum value in nums as the LIS length and sequence
Solution
Step 1: Understand LIS length vs sequence
Length calculation only tracks counts. To get the sequence, we must remember which elements lead to current LIS values.
Step 2: Use predecessor array for reconstruction
Maintain an array storing the index of the previous element in the LIS for each position. After LIS lengths are computed, backtrack from the max index to build the sequence.
Step 3: Reject incorrect options
Sorting changes order, invalid for LIS. Stack method is not standard for LIS. Returning max value is incorrect.
Final Answer:
Keep a predecessor array to track previous indices and reconstruct the sequence after computing LIS lengths -> Option C
Quick Check:
Predecessor array enables LIS reconstruction [OK]
Hint: Track previous indices to rebuild LIS sequence [OK]
Common Mistakes:
Sorting input before LIS calculation
Confusing stack usage with LIS logic
Returning max element instead of sequence
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.