All Posts

Leetcode 2192: All Ancestors of a Node in a Directed Acyclic Graph

You are given a directed acyclic graph (DAG) with n nodes, numbered from 0 to n-1. Along with this, you are given a list of directed edges where each edge [fromi, toi] indicates a directed edge from node fromi to node toi. For each node in the graph, you need to determine the list of all its ancestors. A node u is an ancestor of node v if there is a path from u to v through one or more directed edges. Return a list answer where answer[i] contains the sorted list of ancestors of the i-th node.

Leetcode 2316: Count Unreachable Pairs of Nodes in an Undirected Graph

You are given an undirected graph with n nodes and a list of edges connecting the nodes. The goal is to find the number of pairs of distinct nodes that are unreachable from each other.

Leetcode 2359: Find Closest Node to Given Two Nodes

You are given a directed graph with n nodes, where each node can have at most one outgoing edge. You are provided with an array edges representing the graph, where edges[i] indicates a directed edge from node i to node edges[i] (or -1 if there is no outgoing edge). You are also given two nodes node1 and node2. The task is to return the node that is reachable from both node1 and node2, such that the maximum of the distances from node1 and node2 to that node is minimized. If there are multiple such nodes, return the smallest index, or return -1 if no such node exists.

Leetcode 2368: Reachable Nodes With Restrictions

You are given a tree with n nodes and n - 1 edges, where nodes are labeled from 0 to n - 1. Additionally, a list of restricted nodes is provided. Your goal is to determine the maximum number of nodes that can be visited starting from node 0, without passing through any restricted node. Node 0 itself is not restricted.

Leetcode 2374: Node With Highest Edge Score

You are given a directed graph where each node has exactly one outgoing edge, and the graph is represented by an integer array edges of size n. The value at edges[i] indicates that there is a directed edge from node i to node edges[i]. The edge score of a node i is the sum of all the node labels that have an edge pointing to i. Your task is to find the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index.

Leetcode 2467: Most Profitable Path in a Tree

In a tree with n nodes labeled from 0 to n - 1, each node has a gate with a price to open. Alice starts at node 0 and Bob starts at node bob. Alice moves towards a leaf node, and Bob moves towards node 0. At every node, both Alice and Bob either pay the price to open the gate, or they receive a reward. If they reach a node simultaneously, they share the price/reward equally. Return the maximum net income Alice can achieve if she travels towards the optimal leaf node.