Skip to content

Commit e36e63f

Browse files
committed
04/12/2020
1 parent d2112bb commit e36e63f

File tree

2 files changed

+40
-55
lines changed

2 files changed

+40
-55
lines changed

Daily-challenge/Dec/04/README.md

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,56 @@
1-
# Minimum Height Trees
1+
# The kth Factor of n
2+
Given two positive integers n and k.
23

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.
55

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.
117

128

139

1410
Example 1:
1511

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.
2015
Example 2:
2116

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.
2520
Example 3:
2621

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.
2925
Example 4:
3026

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].
3335

3436

3537
Constraints:
3638

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>
4340

4441
## Idea
4542
It is a really really hard one for me
4643

4744
## Code
4845
```python
4946
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]
6556
```

Daily-challenge/Dec/04/sol.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
class Solution:
2-
def findMinHeightTrees(self, n, edges):
3-
neighbors = collections.defaultdict(set)
4-
for v, w in edges:
5-
neighbors[v].add(w)
6-
neighbors[w].add(v)
7-
def maxpath(v, visited):
8-
visited.add(v)
9-
paths = [maxpath(w, visited) for w in neighbors[v] if w not in visited]
10-
path = max(paths or [[]], key=len)
11-
path.append(v)
12-
return path
13-
path = maxpath(0, set())
14-
path = maxpath(path[0], set())
15-
m = len(path)
16-
return path[(m-1)//2:m//2+1]
2+
def kthFactor(self, n: int, k: int) -> int:
3+
lst = []
4+
5+
for i in range(1, n+1):
6+
if n % i == 0:
7+
lst += [i]
8+
if len(lst) < k:
9+
return -1
10+
return lst[k-1]

0 commit comments

Comments
 (0)