0
0
DSA Typescriptprogramming~10 mins

Longest Increasing Subsequence in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the dp array with 1s for each element.

DSA Typescript
const dp = new Array(nums.length).fill([1]);
Drag options to blanks, or click blank then click option'
A0
B1
Cnums.length
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0 instead of 1
Using nums.length as fill value
2fill in blank
medium

Complete the code to check if nums[j] is less than nums[i] to extend the subsequence.

DSA Typescript
if (nums[j] [1] nums[i]) {
Drag options to blanks, or click blank then click option'
A<
B>=
C===
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <
Using >= which allows equal values
3fill in blank
hard

Fix the error in updating dp[i] to the maximum length found so far.

DSA Typescript
dp[i] = Math.[1](dp[i], dp[j] + 1);
Drag options to blanks, or click blank then click option'
Afloor
Bround
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min which gives the shortest length
Using Math.floor or Math.round which are unrelated
4fill in blank
hard

Fill both blanks to find the maximum length in dp array after processing.

DSA Typescript
const maxLength = dp.[1]((a, b) => a [2] b ? a : b);
Drag options to blanks, or click blank then click option'
Areduce
Bmax
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using max as a method (not valid on arrays)
Using < which finds minimum
5fill in blank
hard

Fill all three blanks to complete the LIS function that returns the length of the longest increasing subsequence.

DSA Typescript
function lengthOfLIS(nums: number[]): number {
  const dp = new Array(nums.length).fill([1]);
  for (let i = 1; i < nums.length; i++) {
    for (let j = 0; j < i; j++) {
      if (nums[j] [2] nums[i]) {
        dp[i] = Math.[3](dp[i], dp[j] + 1);
      }
    }
  }
  return dp.reduce((a, b) => a > b ? a : b);
}
Drag options to blanks, or click blank then click option'
A1
B<
Cmax
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for dp initialization
Using > instead of < in condition
Using Math.min instead of Math.max