I’m doing https://leetcode.com/problems/longest-increasing-subsequence/
I know in theory that each index needs to be assigned a length but i’m having trouble coding it up using my iterative approach
/**
* @param {number[]} nums
* @return {number}
*/
function lengthOfLIS(nums) {
let maxSubsequenceLength = -Infinity;
const stack = [[-1, 0, 0]];
const cache = new Map();
while (stack.length > 0) {
const [previousIndex, currentIndex, currentLength] = stack.pop();
if (currentIndex === nums.length) {
maxSubsequenceLength = Math.max(maxSubsequenceLength, currentLength);
} else {
const previousNumber = nums[previousIndex] ?? -Infinity;
const currentNumber = nums[currentIndex];
if (currentNumber > previousNumber) {
stack.push([currentIndex, currentIndex + 1, currentLength + 1]);
}
stack.push([previousIndex, currentIndex + 1, currentLength]);
}
}
return maxSubsequenceLength;
};