Skip to content

Conversation

@JustDevRae
Copy link
Collaborator

πŸ“Œ ν‘Ό 문제


πŸ“ κ°„λ‹¨ν•œ 풀이 κ³Όμ •

Find the Town Judge

각 μ‚¬λžŒμ˜ μ‹ λ’° 점수λ₯Ό 배열에 μ§‘κ³„ν•œ ν›„, 배열을 μˆœνšŒν•˜λ©° μ‹ λ’° μ μˆ˜κ°€ n - 1인 μ‚¬λžŒμ„ μ°Ύμ•„ λ°˜ν™˜ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

var findJudge = function (n, trust) {
  if (n === 1) return 1;

  const trustScore = new Array(n + 1).fill(0);

  for (const [a, b] of trust) {
    trustScore[a]--;
    trustScore[b]++;
  }

  for (let i = 1; i <= n; i++) {
    if (trustScore[i] === n - 1) return i;
  }

  return -1;
};

Find if Path Exists in Graph

edgesλ₯Ό 인접 리슀트둜 λ³€ν™˜ν•œ ν›„, BFS 큐λ₯Ό ν™œμš©ν•΄ μ‹œμž‘ λ…Έλ“œμ—μ„œ λͺ©ν‘œ λ…Έλ“œκΉŒμ§€ κ²½λ‘œκ°€ μ‘΄μž¬ν•˜λŠ”μ§€λ₯Ό νƒμƒ‰ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

var validPath = function (n, edges, source, destination) {
  const adjacencyList = new Array(n).fill(0).map(() => []);
  for (const [nodeA, nodeB] of edges) {
    adjacencyList[nodeA].push(nodeB);
    adjacencyList[nodeB].push(nodeA);
  }

  const visitedNodes = new Array(n).fill(false);
  const bfsQueue = [source];
  visitedNodes[source] = true;

  while (bfsQueue.length) {
    const currentNode = bfsQueue.shift();
    if (currentNode === destination) return true;

    for (const adjacentNode of adjacencyList[currentNode]) {
      if (!visitedNodes[adjacentNode]) {
        visitedNodes[adjacentNode] = true;
        bfsQueue.push(adjacentNode);
      }
    }
  }

  return false;
};

κ²Œμž„ λ§΅ μ΅œλ‹¨κ±°λ¦¬

미둜 지도λ₯Ό 2차원 λ°°μ—΄λ‘œ ν•΄μ„ν•œ ν›„, 이동 λ°©ν–₯ 배열을 μ΄μš©ν•΄ BFS둜 μ΅œλ‹¨ 경둜λ₯Ό νƒμƒ‰ν•˜κ³  도착 μ§€μ κΉŒμ§€μ˜ 이동 횟수λ₯Ό κ³„μ‚°ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

function solution(maps) {
  const n = maps.length;
  const m = maps[0].length;

  const dx = [0, 0, 1, -1];
  const dy = [1, -1, 0, 0];

  let queue = [];
  queue.push([0, 0]);

  while (queue.length) {
    const [y, x] = queue.shift();

    if (y === n - 1 && x === m - 1) {
      return maps[y][x];
    }

    for (let i = 0; i < 4; i++) {
      const ny = y + dy[i];
      const nx = x + dx[i];

      if (ny >= 0 && ny < n && nx >= 0 && nx < m && maps[ny][nx] === 1) {
        maps[ny][nx] = maps[y][x] + 1;
        queue.push([ny, nx]);
      }
    }
  }

  return -1;
}

νƒ€κ²Ÿ λ„˜λ²„

숫자 배열을 DFS κΈ°λ²•μœΌλ‘œ μž¬κ·€ νƒμƒ‰ν•œ ν›„, λͺ¨λ“  경우의 수 쀑 λͺ©ν‘œ μˆ«μžμ— λ„λ‹¬ν•˜λŠ” 경우λ₯Ό λˆ„μ ν•˜μ—¬ μΉ΄μš΄νŠΈν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

function solution(numbers, target) {
  let count = 0;

  function DFS(index, currentSum) {
    if (index === numbers.length) {
      if (currentSum === target) count++;
      return;
    }

    DFS(index + 1, currentSum + numbers[index]);
    DFS(index + 1, currentSum - numbers[index]);
  }

  DFS(0, 0);

  return count;
}

배달

λ„λ‘œ 정보λ₯Ό κ·Έλž˜ν”„λ‘œ κ΅¬μ„±ν•œ ν›„, λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ ν™œμš©ν•΄ μ‹œμž‘ λ§ˆμ„μ—μ„œ 각 λ§ˆμ„κΉŒμ§€μ˜ μ΅œλ‹¨ 이동 μ‹œκ°„μ„ κ³„μ‚°ν•˜κ³ , μ œν•œ μ‹œκ°„ λ‚΄ 도달 κ°€λŠ₯ν•œ λ§ˆμ„μ˜ 수λ₯Ό μ§‘κ³„ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

function solution(N, road, K) {
  const graph = Array.from({ length: N + 1 }, () => []);
  for (const [a, b, c] of road) {
    graph[a].push({ node: b, cost: c });
    graph[b].push({ node: a, cost: c });
  }

  const distance = new Array(N + 1).fill(Infinity);
  distance[1] = 0;

  const visited = new Array(N + 1).fill(false);

  for (let i = 1; i <= N; i++) {
    let current = -1;

    for (let j = 1; j <= N; j++) {
      if (!visited[j] && (current === -1 || distance[j] < distance[current])) {
        current = j;
      }
    }

    if (current === -1) break;

    visited[current] = true;

    for (const { node, cost } of graph[current]) {
      if (distance[node] > distance[current] + cost) {
        distance[node] = distance[current] + cost;
      }
    }
  }
  return distance.filter((time) => time <= K).length;
}

Copy link
Collaborator

@Moonjonghoo Moonjonghoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ „μ²΄μ μœΌλ‘œ 탐색과 배열을 잘 ν™œμš©ν•΄μ„œ ν’€μ–΄μ£Όμ‹ κ±°κ°™μŠ΅λ‹ˆλ‹€!
κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@bona1122 bona1122 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ½”λ“œ 잘 λ³΄μ•˜μŠ΅λ‹ˆλ‹€. dfs,bfs,λ‹€μ΅μŠ€νŠΈλΌ λͺ¨λ‘ 잘 μ΄μš©ν•˜μ‹  것 κ°™μŠ΅λ‹ˆλ‹€! ν•œμ£Όλ™μ•ˆ μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€πŸ˜Š

@JooKangsan JooKangsan merged commit a495e21 into main Feb 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants