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

179
Views
What's wrong with my code for leetcode 2246?

I am looking at LeetCode problem 2246. Longest Path With Different Adjacent Characters:

You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.

You are also given a string s of length n, where s[i] is the character assigned to node i.

Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.

This is my code:

function longestPath(parent: number[], s: string): number {
  const childrenMap: Record<number, number[]> = {}
  for (let node = 0; node < parent.length; node++) {
    const parentNode = parent[node]
    childrenMap[parentNode] = childrenMap[parentNode] ?? []
    childrenMap[parentNode].push(node)
  }
  let max = 0
  const dfs = (node = 0, parentNode = -1): number => {
    if (s[node] === s[parentNode]) {
      max = Math.max(max, dfs(node, -1))
      return 0
    }

    const [a = 0, b = 0] = (childrenMap[node] ?? []).map((child) => dfs(child, node))
      .sort((la, lb) => lb - la)
    max = Math.max(max, 1 + a + b)
    return a + 1
  }
  dfs()
  return max
}

console.log(
  longestPath(
    [-1, 56, 10, 79, 52, 0, 37, 39, 127, 125, 116, 52, 95, 131, 105, 55, 55, 52, 87, 35, 43, 130, 87, 103, 8, 73, 8, 116, 4, 43, 60, 104, 116, 118, 78, 9, 133, 139, 7, 127, 96, 28, 52, 79, 78, 36, 102, 134, 100, 104, 47, 127, 129, 77, 121, 133, 10, 58, 104, 55, 69, 73, 107, 9, 139, 79, 52, 72, 130, 78, 112, 43, 14, 4, 120, 9, 139, 118, 52, 52, 73, 82, 79, 58, 121, 80, 139, 10, 25, 74, 10, 123, 134, 112, 40, 80, 108, 128, 5, 52, 43, 31, 10, 42, 79, 139, 86, 58, 3, 118, 117, 21, 4, 79, 45, 26, 5, 122, 102, 13, 88, 139, 108, 118, 116, 10, 58, 32, 80, 125, 121, 105, 116, 104, 82, 131, 39, 10, 126, 125],
    'vodpyvpjmogqvwnibqasbulkfbfugvtlpdtsmydrbrekavkhifoypepbcnzpmasbnlrfqgdhnmvhldsrogjsntummchcftzrnycichziopmphfqwqdsihoywdpqqkyvzrhbqorwrkmns',
  ),
)

It outputs 13, but the expected output is 17.

When I replace

 max = Math.max(max, dfs(node, -1))

with

max = Math.max(dfs(node, -1), max)

the answer will be right (17). I can't tell the difference... What's wrong?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your dfs changes max in two places:

  max = Math.max(
    max,
    dfs(node, -1),
  )
...
max = Math.max(max, 1 + a + b)
return a + 1

So the order in which you call dfs as an argument to Math.max can affect things:

  1. If you call dfs first, it will change max and the second argument passed to Math.max will be the changed value, so it will compare with the correct value that was changed by the recursive dfs calls.
  2. If you call dfs second, it will copy max, call dfs then compare it with the old max, with the value it had before the recursive call.

Solution:

  1. The best thing you can do is not mutate state external to dfs: make dfs return what you need and use that returned value, don't rely on an external variable. You seem to already be returning something from dfs, so it shouldn't be too hard to get rid of mutating max.

  2. That can be messy in some cases (but not yours), in those cases at least don't put the recursive call as a parameter. Do it like this:

    const dfsResult = dfs(node, -1)
    max = Math.max(max, dfsResult)
    
about 4 years ago · Santiago Trujillo 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!