title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
Python 3 || 14 lines, w/ analysis || T/M: 1647 ms / 55.9 MB (100/100)
add-edges-to-make-degrees-of-all-nodes-even
0
1
Here\'s the analysis:\n\n- Applying one edge fixes exactly two odd edges. Therefore the only fixable graphs are those with either two or four odd nodes.\n\n- A graph with anything other than zero, two, or four odd nodes cannot be fixed; the answer in those cases is `False`.\n- Case 0: Obviously`True`.\n- Case 2: Suppos...
4
There is an **undirected** graph consisting of `n` nodes numbered from `1` to `n`. You are given the integer `n` and a **2D** array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi`. The graph can be disconnected. You can add **at most** two additional edges (possibly none...
null
Python Hard
add-edges-to-make-degrees-of-all-nodes-even
0
1
```\nclass Solution:\n def isPossible(self, n: int, edges: List[List[int]]) -> bool:\n \'\'\'\n You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.\n\nReturn true if it is possible to make the degree of each node in the graph eve...
0
There is an **undirected** graph consisting of `n` nodes numbered from `1` to `n`. You are given the integer `n` and a **2D** array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi`. The graph can be disconnected. You can add **at most** two additional edges (possibly none...
null
Python & C++ | O(mn) time | Find common ancestor with explanation
cycle-length-queries-in-a-tree
0
1
The description of the problem is quite long but you basically need to find to the common ancestor of nodes in each query.\nThe problem is very similar to https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/\n\nHere we do not even need to track parents for each child (and use dict as in the similar p...
3
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
Easy python Solution.
cycle-length-queries-in-a-tree
0
1
\n\n# Code\n```\nclass Solution:\n def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]:\n res=[]\n for x,y in queries:\n ans=1\n while x!=y:\n if x>y:\n x//=2\n else:\n y//=2\n ...
1
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
[C++|Java|Python3] climbing up
cycle-length-queries-in-a-tree
1
1
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/af6e415cd101768ea8743ea9e4d22d788c9461c3) for solutions of weekly 324. \n\n**Intuition**\nSince this is a complete tree, it is guaranteed to be shallow. We can compute the result of each query in a brute-force approach by visiting the parent of the...
1
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
[Python3] Lowest Common Ancestor
cycle-length-queries-in-a-tree
0
1
# Code\n```\nclass Solution:\n def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]:\n ans = []\n for a, b in queries:\n path_a, path_b = self.getPath(a), self.getPath(b)\n set_a, set_b = set(path_a), set(path_b)\n cnt = 0\n for num in ...
1
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
[python3] lca sol for reference
cycle-length-queries-in-a-tree
0
1
T: O(H * Q)\nS: O(Q)\n\nH = log2(n + 1)\n\n```\nclass Solution:\n def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]:\n ans = []\n for s, e in queries: \n p = s\n q = e\n a = 0\n \n while p != q: \n while p ...
1
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
Python 3 || 9 lines, w/ brief explanation || T/M: 81%/91%
cycle-length-queries-in-a-tree
0
1
Here\'s the plan:\n- The parent of node`n`is `n//2`. \n- Given two query nodes`a`and`b`, we can select the larger of`a`or`b`and int-divide it by two in each step. We count these steps, and we stop when `a`and`b`are equal.\n- We initialize the step count`moves`as 1 to account for the edge between the nodes given in the ...
5
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
[Python | Time & Space Beats 100%] O(1) Time Complexity for Each Query, Super Easy Implementation.
cycle-length-queries-in-a-tree
0
1
# Intuition\nBinary Encoding: Given the context of this question, actually its much more easier to understand it in binary encoding:\n\nFor example, node #6, can be written in binary as 0b110, and can be interprated as 1(right) -> 1(right) -> 0(left). Following this intuition, given two nodes, for example #12(0b1100) a...
1
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
Simple O(n) Single-pass solution
cycle-length-queries-in-a-tree
0
1
Given the properties of the tree in question, node with value **n** has parent value **floor(n/2)**. This can be used to traverse upwards in the tree from both nodes (while keeping track of distance traveled) until a common ancestor is found. This distance will represent the length of the cycle less the single edge con...
0
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null
easy
cycle-length-queries-in-a-tree
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: * The left node has the value `2 * val`, and * The right node has the value `2 * val ...
null