Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
When adding cache to my longest increasing subsequence solution, answer is not correct?

I am working on https://leetcode.com/problems/longest-increasing-subsequence

For test case

input: [5, 4, 19, 5, 7, 12]; output: 4

Without the cache, I am able to output 4 correctly.

var lengthOfLIS = function (ns) {
  if (ns.length === 1) return 1;

  const prev_i = -1;
  const curr_i = 0;
  const res = 0;
  return recur(ns, prev_i, curr_i, res);
};

// good
var recur = function (ns, prev_i, curr_i, res) {
  //
  if (curr_i >= ns.length) {
    return res;
  }

  // take
  let out_1 = 0;
  if (prev_i === -1) {
    out_1 = recur(ns, curr_i, curr_i + 1, res + 1);
  } else if (ns[curr_i] > ns[prev_i]) {
    out_1 = recur(ns, curr_i, curr_i + 1, res + 1);
  } else if (ns[curr_i] <= ns[prev_i]) {
    out_1 = res;
  }

  // !take
  let out_2 = 0;
  out_2 = recur(ns, prev_i, curr_i + 1, res);

  const max = Math.max(out_1, out_2);
  return max;
};

When I adding cache to it, my output is 3.

var recur = function (dp, ns, prev_i, curr_i, res) {
  //
  if (curr_i >= ns.length) {
    return res;
  }

  if (prev_i !== -1 && dp[prev_i + 1][curr_i + 1] !== undefined) {
    return dp[prev_i + 1][curr_i + 1];
  }

  // take
  let out_1 = 0;
  if (prev_i === -1) {
    out_1 = recur(dp, ns, curr_i, curr_i + 1, res + 1);
  } else if (ns[curr_i] > ns[prev_i]) {
    out_1 = recur(dp, ns, curr_i, curr_i + 1, res + 1);
  } else if (ns[curr_i] <= ns[prev_i]) {
    out_1 = res;
  }

  // !take
  let out_2 = 0;
  out_2 = recur(dp, ns, prev_i, curr_i + 1, res);

  const max = Math.max(out_1, out_2);
  dp[prev_i + 1][curr_i + 1] = max;
  return max;
};

var lengthOfLIS = function (ns) {
  if (ns.length === 1) return 1;

  const dp = Array(ns.length + 1)
    .fill(undefined)
    .map((_, i) => {
      return Array(ns.length + 1).fill(undefined);
    });

  const prev_i = -1;
  const curr_i = 0;
  const res = 0;
  return recur(dp, ns, prev_i, curr_i, res);
};

Could someone point out any mistake I made? I am trying to use debugger to see what is happening.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can debug this kind of problem by comparing the cached result with the uncached result and logging differences.

The problem is that the result of this function depends on the values of prev_i, curr_i, and res. However, your cache only depends on values of prev_i and curr_i so does not have enough information to predict the correct output.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!