Skip to content

Commit b1c9563

Browse files
committed
idk
1 parent 4c2b7fc commit b1c9563

File tree

4 files changed

+78
-104
lines changed

4 files changed

+78
-104
lines changed

Daily-challenge/2021/Jan/02/README.md

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,27 @@ target node is a node from the original tree and is not null.<br>
4949
two ways, one is to use a list and cheat, or we can use standard while loop and extra space for the dummy nodes and etc.
5050
## Code
5151
```python
52-
# Definition for singly-linked list.
53-
# class ListNode:
54-
# def __init__(self, val=0, next=None):
55-
# self.val = val
56-
# self.next = next
57-
class Solution:
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, x):
55+
# self.val = x
56+
# self.left = None
57+
# self.right = None
5858

59-
def __init__(self, head: ListNode):
60-
"""
61-
@param head The linked list's head.
62-
Note that the head is guaranteed to be not null, so it contains at least one node.
63-
"""
64-
self.range = []
65-
while head:
66-
self.range.append(head.val)
67-
head = head.next
59+
class Solution:
60+
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
61+
if not original or not cloned:
62+
return None
63+
stack = []
64+
stack += [(original,cloned)]
65+
while stack:
66+
org, clone = stack.pop()
6867

69-
70-
def getRandom(self) -> int:
71-
"""
72-
Returns a random node's value.
73-
"""
74-
num = int(random.random() * len(self.range))
75-
return self.range[num]
76-
77-
78-
79-
# Your Solution object will be instantiated and called as such:
80-
# obj = Solution(head)
81-
# param_1 = obj.getRandom()
68+
if org == target:
69+
return clone
70+
if org.left:
71+
stack.append((org.left, clone.left))
72+
if org.right:
73+
stack.append((org.right, clone.right))
8274

8375
```

Daily-challenge/2021/Jan/02/sol.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
6-
class Solution:
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
77

8-
def __init__(self, head: ListNode):
9-
"""
10-
@param head The linked list's head.
11-
Note that the head is guaranteed to be not null, so it contains at least one node.
12-
"""
13-
self.range = []
14-
while head:
15-
self.range.append(head.val)
16-
head = head.next
8+
class Solution:
9+
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
10+
if not original or not cloned:
11+
return None
12+
stack = []
13+
stack += [(original,cloned)]
14+
while stack:
15+
org, clone = stack.pop()
1716

18-
19-
def getRandom(self) -> int:
20-
"""
21-
Returns a random node's value.
22-
"""
23-
num = int(random.random() * len(self.range))
24-
return self.range[num]
25-
26-
27-
28-
# Your Solution object will be instantiated and called as such:
29-
# obj = Solution(head)
30-
# param_1 = obj.getRandom()
17+
if org == target:
18+
return clone
19+
if org.left:
20+
stack.append((org.left, clone.left))
21+
if org.right:
22+
stack.append((org.right, clone.right))

Daily-challenge/2021/Jan/03/README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
# Increasing Order Search Tree
2-
Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
1+
# Beautiful Arrangement
2+
3+
Solution
4+
Suppose you have n integers labeled 1 through n. A permutation of those n integers perm (1-indexed) is considered a beautiful arrangement if for every i (1 <= i <= n), either of the following is true:
5+
6+
perm[i] is divisible by i.
7+
i is divisible by perm[i].
8+
Given an integer n, return the number of the beautiful arrangements that you can construct.
39

410

511

612
Example 1:
713

8-
9-
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
10-
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
14+
Input: n = 2
15+
Output: 2
16+
Explanation:
17+
The first beautiful arrangement is [1,2]:
18+
- perm[1] = 1 is divisible by i = 1
19+
- perm[2] = 2 is divisible by i = 2
20+
The second beautiful arrangement is [2,1]:
21+
- perm[1] = 2 is divisible by i = 1
22+
- i = 2 is divisible by perm[2] = 1
1123
Example 2:
1224

13-
14-
Input: root = [5,1,7]
15-
Output: [1,null,5,null,7]
25+
Input: n = 1
26+
Output: 1
1627

1728

1829
Constraints:
1930

20-
The number of nodes in the given tree will be in the range [1, 100].
21-
0 <= Node.val <= 1000 <br>
31+
1 <= n <= 15<br>
2232

2333
## Idea
2434
Just a while loop or for loop is fine
2535

2636
## Code
2737

2838
```python
29-
# if this null node was a left child, tail is its parent
30-
# if this null node was a right child, tail is its parent's parent
31-
if not root: return tail
32-
33-
# recursive call, traversing left while passing in the current node as tail
34-
res = self.increasingBST(root.left, root)
35-
36-
# we don't want the current node to have a left child, only a single right child
37-
root.left = None
38-
39-
# we set the current node's right child to be tail
40-
# what is tail? this part is important
41-
# if the current node is a left child, tail will be its parent
42-
# else if the current node is a right child, tail will be its parent's parent
43-
root.right = self.increasingBST(root.right, tail)
44-
45-
# throughout the whole algorithm, res is the leaf of the leftmost path in the original tree
46-
# its the smallest node and thus will be the root of the modified tree
47-
return res
39+
class Solution:
40+
def countArrangement(self, N: int) -> int:
41+
def count(i, X):
42+
if i == 1:
43+
return 1
44+
return sum(count(i - 1, X - {x})
45+
for x in X
46+
if x % i == 0 or i % x == 0)
47+
return count(N, set(range(1, N + 1)))
4848
```

Daily-challenge/2021/Jan/03/sol.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
# if this null node was a left child, tail is its parent
2-
# if this null node was a right child, tail is its parent's parent
3-
if not root: return tail
4-
5-
# recursive call, traversing left while passing in the current node as tail
6-
res = self.increasingBST(root.left, root)
7-
8-
# we don't want the current node to have a left child, only a single right child
9-
root.left = None
10-
11-
# we set the current node's right child to be tail
12-
# what is tail? this part is important
13-
# if the current node is a left child, tail will be its parent
14-
# else if the current node is a right child, tail will be its parent's parent
15-
root.right = self.increasingBST(root.right, tail)
16-
17-
# throughout the whole algorithm, res is the leaf of the leftmost path in the original tree
18-
# its the smallest node and thus will be the root of the modified tree
19-
return res
1+
class Solution:
2+
def countArrangement(self, N: int) -> int:
3+
def count(i, X):
4+
if i == 1:
5+
return 1
6+
return sum(count(i - 1, X - {x})
7+
for x in X
8+
if x % i == 0 or i % x == 0)
9+
return count(N, set(range(1, N + 1)))

0 commit comments

Comments
 (0)