Skip to content

Commit 95b54e6

Browse files
committed
feat: add No.310,752
1 parent fdb410a commit 95b54e6

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

301-400/310. Minimum Height Trees.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 310. Minimum Height Trees
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort.
5+
- Similar Questions: Course Schedule, Course Schedule II, Collect Coins in a Tree, Count Pairs of Connectable Servers in a Weighted Tree Network.
6+
7+
## Problem
8+
9+
A tree is an undirected graph in which any two vertices are connected by *exactly* one path. In other words, any connected graph without simple cycles is a tree.
10+
11+
Given a tree of `n` nodes labelled from `0` to `n - 1`, and an array of `n - 1` `edges` where `edges[i] = [ai, bi]` indicates that there is an undirected edge between the two nodes `ai` and `bi` in the tree, you can choose any node of the tree as the root. When you select a node `x` as the root, the result tree has height `h`. Among all possible rooted trees, those with minimum height (i.e. `min(h)`)  are called **minimum height trees** (MHTs).
12+
13+
Return **a list of all **MHTs'** root labels**. You can return the answer in **any order**.
14+
15+
The **height** of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
16+
17+
 
18+
Example 1:
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/01/e1.jpg)
21+
22+
```
23+
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
24+
Output: [1]
25+
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
26+
```
27+
28+
Example 2:
29+
30+
![](https://assets.leetcode.com/uploads/2020/09/01/e2.jpg)
31+
32+
```
33+
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
34+
Output: [3,4]
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `1 <= n <= 2 * 104`
43+
44+
- `edges.length == n - 1`
45+
46+
- `0 <= ai, bi < n`
47+
48+
- `ai != bi`
49+
50+
- All the pairs `(ai, bi)` are distinct.
51+
52+
- The given input is **guaranteed** to be a tree and there will be **no repeated** edges.
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number} n
61+
* @param {number[][]} edges
62+
* @return {number[]}
63+
*/
64+
var findMinHeightTrees = function(n, edges) {
65+
if (!edges.length) return [0];
66+
var map = Array(n).fill(0).map(() => new Map());
67+
for (var i = 0; i < edges.length; i++) {
68+
map[edges[i][0]].set(edges[i][1], true);
69+
map[edges[i][1]].set(edges[i][0], true);
70+
}
71+
var arr = [];
72+
for (var i = 0; i < n; i++) {
73+
if (map[i].size === 1) {
74+
arr.push(i);
75+
}
76+
}
77+
while (n > 2) {
78+
n -= arr.length;
79+
var newArr = [];
80+
for (var i = 0; i < arr.length; i++) {
81+
var j = Array.from(map[arr[i]].keys())[0];
82+
map[j].delete(arr[i]);
83+
if (map[j].size === 1) {
84+
newArr.push(j);
85+
}
86+
}
87+
arr = newArr;
88+
}
89+
return arr;
90+
};
91+
```
92+
93+
**Explain:**
94+
95+
Delete every leaf untill there is only one or two nodes.
96+
97+
**Complexity:**
98+
99+
* Time complexity : O(n).
100+
* Space complexity : O(n).

701-800/752. Open the Lock.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 752. Open the Lock
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table, String, Breadth-First Search.
5+
- Similar Questions: Reachable Nodes With Restrictions.
6+
7+
## Problem
8+
9+
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot.
10+
11+
The lock initially starts at `'0000'`, a string representing the state of the 4 wheels.
12+
13+
You are given a list of `deadends` dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
14+
15+
Given a `target` representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
22+
Output: 6
23+
Explanation:
24+
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
25+
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
26+
because the wheels of the lock become stuck after the display becomes the dead end "0102".
27+
```
28+
29+
Example 2:
30+
31+
```
32+
Input: deadends = ["8888"], target = "0009"
33+
Output: 1
34+
Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
35+
```
36+
37+
Example 3:
38+
39+
```
40+
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
41+
Output: -1
42+
Explanation: We cannot reach the target without getting stuck.
43+
```
44+
45+
 
46+
**Constraints:**
47+
48+
49+
50+
- `1 <= deadends.length <= 500`
51+
52+
- `deadends[i].length == 4`
53+
54+
- `target.length == 4`
55+
56+
- target **will not be** in the list `deadends`.
57+
58+
- `target` and `deadends[i]` consist of digits only.
59+
60+
61+
62+
## Solution
63+
64+
```javascript
65+
/**
66+
* @param {string[]} deadends
67+
* @param {string} target
68+
* @return {number}
69+
*/
70+
var openLock = function(deadends, target) {
71+
if (target === '0000') return 0;
72+
var arr = ['0000'];
73+
var visited = { '0000': true };
74+
var deadendsMap = {};
75+
for (var i = 0; i < deadends.length; i++) {
76+
if (deadends[i] === '0000') return -1;
77+
deadendsMap[deadends[i]] = true;
78+
}
79+
var res = 0;
80+
while (arr.length) {
81+
var newArr = [];
82+
res += 1;
83+
for (var i = 0; i < arr.length; i++) {
84+
var nexts = getNexts(arr[i]);
85+
for (var j = 0; j < nexts.length; j++) {
86+
if (nexts[j] === target) return res;
87+
if (!visited[nexts[j]] && !deadendsMap[nexts[j]]) {
88+
newArr.push(nexts[j]);
89+
visited[nexts[j]] = true;
90+
}
91+
}
92+
}
93+
arr = newArr;
94+
}
95+
return -1;
96+
};
97+
98+
var getNexts = function(str) {
99+
var res = [];
100+
for (var i = 0; i < str.length; i++) {
101+
var num = Number(str[i]);
102+
res.push(str.slice(0, i) + (num === 9 ? 0 : num + 1) + str.slice(i + 1));
103+
res.push(str.slice(0, i) + (num === 0 ? 9 : num - 1) + str.slice(i + 1));
104+
}
105+
return res;
106+
};
107+
```
108+
109+
**Explain:**
110+
111+
nope.
112+
113+
**Complexity:**
114+
115+
* Time complexity : O(n).
116+
* Space complexity : O(n).

0 commit comments

Comments
 (0)