[LeetCode] 399. Evaluate Division, Medium
·
CodingTest/LeetCode
1. 문제equitions에 문자열 2차원 배열이 주어지고, 각 문자열을 나눈 값을 담은 배열 values가 주어질 때, queries의 나눈 값을 담은 배열을 반환하라.존재할 수 없는 경우는 -1.0을 담아야 한다.2. 해결function calcEquation( equations: string[][], values: number[], queries: string[][]): number[] { const graph = new Map(); // 그래프 만들기 for (let i = 0; i b (value) graph.get(b)!.push([a, 1 / value]); // b -> a (1/value) } // DFS 함수 const df..
[LeetCode] 1466. Reorder Routes to Make All Paths Lead to the City Zero, Medium
·
CodingTest/LeetCode
1. 문제도시의 수 n이 주어지고, 도시 간의 연결 도로 정보 2차원 배열이 주어질 때 모든 도시에서 0번 도시로 갈 수 있도록 변경해야하는 도로의 수를 반환하라.2. 해결function minReorder(n: number, connections: number[][]): number { const visited = new Array(n).fill(false); const map = new Map(); let count = 0; for (const [from, to] of connections) { map.set(from, (map.get(from) || []).concat([[to, 1]])); map.set(to, (map.get(to) || []).c..
[LeetCode] 547. Number of Provinces, Medium
·
CodingTest/LeetCode
1. 문제2차원 배열의 좌표로 각 도시의 연결 정보가 주어질 때, 직접 혹은 간접으로 연결된 그룹을 한 개로 보고 존재하는 지방 도시의 개수를 구해라.2. 해결function findCircleNum(isConnected: number[][]): number { let count = 0; function DFS(y: number) { for(let i =0; i 좌표를 탐색하면서, 연결 정보가 있으면 타고, 타고 올라가서 연결 정보를 모두 지운다.이 과정이 끊나면 한 그룹의 탐색이 끝난 것이므로 count 증가.뭔가 2중 for문이라 리팩토링할 수 있을 거 같은데..
[LeetCode] 1161. Maximum Level Sum of a Binary Tree, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어지고, root부터 레벨1 그 자식이 레벨2와 같이 레벨이 점점 깊어질 때, 해당 레벨의 합이 가장 큰 레벨을 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? nul..
[LeetCode] 199. Binary Tree Right Side View, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어질 때, 오른쪽에서 바라볼 때 보이는 노드의 값을 담은 배열을 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.ri..
[LeetCode] 1372. Longest ZigZag Path in a Binary Tree, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어질 때, 노드가 좌 우 반복하면서 이어지는 경로의 최대 길이를 구하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.rig..
[LeetCode] 437. Path Sum III, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어질 때, 경로 상의 합이 targetSum과 같아지는 경우의 수를 구하라.단, 경로는 root부터 시작해서 leaf에서 끝날 필요가 없고, 중간부터 탐색할 수도 있다. 2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.lef..
[LeetCode] 1448. Count Good Nodes in Binary Tree, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어질 때, 자신까지 오는 경로 상에 자신보다 큰 값이 없었던 경우는 good 노드라 할 수 있다.이진 트리 내에 존재하는 good 노드의 개수를 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left..
[LeetCode] 872. Leaf-Similar Trees, Easy
·
CodingTest/LeetCode
1. 문제이진 트리 2개가 주어질 때, leaf 노드가 동일하면 true 아니면 false를 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this..
[LeetCode] 2130. Maximum Twin Sum of a Linked List, Medium
·
CodingTest/LeetCode
1. 문제짝수 개의 노드로 이루어진 연결 리스트가 주어질 때, 특정 노드의 인덱스와 쌍을 이루는 노드와의 최대 값을 구해라.쌍은 n - 1 - index 위치가 현재 노드의 쌍.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function rev..