Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda