|
1 |
| -# Minimum Height Trees |
| 1 | +# The kth Factor of n |
| 2 | +Given two positive integers n and k. |
2 | 3 |
|
3 |
| -Solution |
4 |
| -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. |
| 4 | +A factor of an integer n is defined as an integer i where n % i == 0. |
5 | 5 |
|
6 |
| -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). |
7 |
| - |
8 |
| -Return a list of all MHTs' root labels. You can return the answer in any order. |
9 |
| - |
10 |
| -The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf. |
| 6 | +Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors. |
11 | 7 |
|
12 | 8 |
|
13 | 9 |
|
14 | 10 | Example 1:
|
15 | 11 |
|
16 |
| - |
17 |
| -Input: n = 4, edges = [[1,0],[1,2],[1,3]] |
18 |
| -Output: [1] |
19 |
| -Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT. |
| 12 | +Input: n = 12, k = 3 |
| 13 | +Output: 3 |
| 14 | +Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. |
20 | 15 | Example 2:
|
21 | 16 |
|
22 |
| - |
23 |
| -Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]] |
24 |
| -Output: [3,4] |
| 17 | +Input: n = 7, k = 2 |
| 18 | +Output: 7 |
| 19 | +Explanation: Factors list is [1, 7], the 2nd factor is 7. |
25 | 20 | Example 3:
|
26 | 21 |
|
27 |
| -Input: n = 1, edges = [] |
28 |
| -Output: [0] |
| 22 | +Input: n = 4, k = 4 |
| 23 | +Output: -1 |
| 24 | +Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1. |
29 | 25 | Example 4:
|
30 | 26 |
|
31 |
| -Input: n = 2, edges = [[0,1]] |
32 |
| -Output: [0,1] |
| 27 | +Input: n = 1, k = 1 |
| 28 | +Output: 1 |
| 29 | +Explanation: Factors list is [1], the 1st factor is 1. |
| 30 | +Example 5: |
| 31 | + |
| 32 | +Input: n = 1000, k = 3 |
| 33 | +Output: 4 |
| 34 | +Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000]. |
33 | 35 |
|
34 | 36 |
|
35 | 37 | Constraints:
|
36 | 38 |
|
37 |
| -1 <= n <= 2 * 104 |
38 |
| -edges.length == n - 1 |
39 |
| -0 <= ai, bi < n |
40 |
| -ai != bi |
41 |
| -All the pairs (ai, bi) are distinct. |
42 |
| -The given input is guaranteed to be a tree and there will be no repeated edges. <br> |
| 39 | +1 <= k <= n <= 1000 <br> |
43 | 40 |
|
44 | 41 | ## Idea
|
45 | 42 | It is a really really hard one for me
|
46 | 43 |
|
47 | 44 | ## Code
|
48 | 45 | ```python
|
49 | 46 | class Solution:
|
50 |
| - def findMinHeightTrees(self, n, edges): |
51 |
| - neighbors = collections.defaultdict(set) |
52 |
| - for v, w in edges: |
53 |
| - neighbors[v].add(w) |
54 |
| - neighbors[w].add(v) |
55 |
| - def maxpath(v, visited): |
56 |
| - visited.add(v) |
57 |
| - paths = [maxpath(w, visited) for w in neighbors[v] if w not in visited] |
58 |
| - path = max(paths or [[]], key=len) |
59 |
| - path.append(v) |
60 |
| - return path |
61 |
| - path = maxpath(0, set()) |
62 |
| - path = maxpath(path[0], set()) |
63 |
| - m = len(path) |
64 |
| - return path[(m-1)//2:m//2+1] |
| 47 | + def kthFactor(self, n: int, k: int) -> int: |
| 48 | + lst = [] |
| 49 | + |
| 50 | + for i in range(1, n+1): |
| 51 | + if n % i == 0: |
| 52 | + lst += [i] |
| 53 | + if len(lst) < k: |
| 54 | + return -1 |
| 55 | + return lst[k-1] |
65 | 56 | ```
|
0 commit comments