Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Leetcode: 1496 PATHS CROSSING JS

https://leetcode.com/problems/path-crossing/

I am working on this leetcode problem and I think I'm close, but I can't figure out why it isn't working. In my IF statement it doesn't seem like i is incrementing. So once it goes through the loop again, it just returns true and exits loop. Once this is working I intend to into one single conditional so it's not so long, but I want to make it work first haha. Any insight?

var isPathCrossing = function (path) {
  const points = {};
  const directions = path.split('').map((d) => {
    return d;
  });
  let x = 0;
  let y = 0;
  let i = 0;
  while (i <= directions.length) {
    const coordinate = `${x}, ${y}`;
    if (directions[i] == 'N') {
      y++;
      if (!Object.values(points).includes(coordinate)) {
            points[i] = coordinate;
            i++;
      } else {
            return true;
      }
    }
    if (directions[i] == 'W') {
      x--;
      if (!Object.values(points).includes(coordinate)) {
            points[i] = coordinate;
            i++;
      } else {
            return true;
      }
    }
    if (directions[i] == 'S') {
      y--;
      if (!Object.values(points).includes(coordinate)) {
            points[i] = coordinate;
            i++;
      } else {
            return true;
      }
    }
    if (directions[i] == 'E') {
      x++;
      if (!Object.values(points).includes(coordinate)) {
            points[i] = coordinate;
            i++;
      } else {
            return true;
      }
    }
  }
  return false;
};

console.log(isPathCrossing('NNNNN'));
console.log(isPathCrossing('NES'));
console.log(isPathCrossing('NESWNEENW'));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Observations

  • (i <= directions.length) refactored to (i < directions.length), as would not want to traverse more than length.
  • With object used for keeping previous coordinate this state { '0': '0, 1', '1': '1, 1', '2': '1, 0', '3': '0, 0' } generated for input "NESWW". Using a set would be more appropriate. (Commented alongside your implementation).

Accepted Solution

var isPathCrossing = function (path) {

    // const points = new Set();
    const points = {};
    const directions = path.split('').map((d) => { return d; });
    
    let x = 0;
    let y = 0;
    let i = 0;
    
    const initCoord = `${x}, ${y}`;
    points[-1] = initCoord;
    // points.add(initCoord);
    
    while (i < directions.length) {
      
        if (directions[i] == 'N') { y++; }
        else if (directions[i] == 'W') { x--; }
        else if (directions[i] == 'S') { y--; }
        else if (directions[i] == 'E') { x++; }
        
        const coordinate = `${x}, ${y}`;

        // console.log(coordinate);
        // console.log(points);

        if (!Object.values(points).includes(coordinate)) {
            points[i] = coordinate;
            i++;
        } else {
            return true;
        }

        // if (!points.has(coordinate)) {
        //     points.add(coordinate);
        //     i++;
        // } else {
        //     return true;
        // }
    }
    return false;
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda