Skip to content

Commit a593bab

Browse files
committed
09/12/2020
1 parent d331b73 commit a593bab

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed

Daily-challenge/Dec/08/README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,19 @@ Constraints:
2929

3030
## Code
3131
```python
32-
# Definition for a binary tree node.
33-
# class TreeNode:
34-
# def __init__(self, val=0, left=None, right=None):
35-
# self.val = val
36-
# self.left = left
37-
# self.right = right
3832
class Solution:
39-
def findTilt(self, root: TreeNode) -> int:
40-
41-
def value(node):
42-
if not node:
43-
return 0
33+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
34+
35+
count = 0
36+
d = collections.defaultdict(int)
37+
4438

45-
left = value(node.left)
46-
right = value(node.right)
47-
tilt = abs(left-right)
48-
self.tilt += tilt
49-
return left + right + node.val
50-
self.tilt = 0
51-
value(root)
52-
53-
return self.tilt
39+
for item in time:
40+
if item % 60 == 0:
41+
count += d[0]
42+
else:
43+
count += d[60-item%60]
44+
d[item%60] += 1
45+
46+
return count
5447
```

Daily-challenge/Dec/08/sol.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
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
61
class Solution:
7-
def getDecimalValue(self, head: ListNode) -> int:
8-
ans = ''
9-
while head:
10-
ans += str(head.val)
11-
head = head.next
12-
return int(ans, 2)
2+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
3+
4+
count = 0
5+
d = collections.defaultdict(int)
6+
7+
8+
for item in time:
9+
if item % 60 == 0:
10+
count += d[0]
11+
else:
12+
count += d[60-item%60]
13+
d[item%60] += 1
14+
15+
return count

Daily-challenge/Dec/09/README.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
1-
# Maximum Difference Between Node and Ancestor
2-
Given the root of a binary tree, find the maximum value V for which there exist different nodes A and B where V = |A.val - B.val| and A is an ancestor of B.
1+
# Binary Search Tree Iterator
2+
Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
33

4-
A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.
4+
BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
5+
boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
6+
int next() Moves the pointer to the right, then returns the number at the pointer.
7+
Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.
8+
9+
You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.
510

611

712

813
Example 1:
914

1015

11-
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
12-
Output: 7
13-
Explanation: We have various ancestor-node differences, some of which are given below :
14-
|8 - 3| = 5
15-
|3 - 7| = 4
16-
|8 - 1| = 7
17-
|10 - 13| = 3
18-
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
19-
Example 2:
20-
16+
Input
17+
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
18+
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
19+
Output
20+
[null, 3, 7, true, 9, true, 15, true, 20, false]
2121

22-
Input: root = [1,null,2,null,0,3]
23-
Output: 3
22+
Explanation
23+
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
24+
bSTIterator.next(); // return 3
25+
bSTIterator.next(); // return 7
26+
bSTIterator.hasNext(); // return True
27+
bSTIterator.next(); // return 9
28+
bSTIterator.hasNext(); // return True
29+
bSTIterator.next(); // return 15
30+
bSTIterator.hasNext(); // return True
31+
bSTIterator.next(); // return 20
32+
bSTIterator.hasNext(); // return False
2433

2534

2635
Constraints:
2736

28-
The number of nodes in the tree is in the range [2, 5000].
29-
0 <= Node.val <= 105<br>
37+
The number of nodes in the tree is in the range [1, 105].
38+
0 <= Node.val <= 106
39+
At most 105 calls will be made to hasNext, and next.
40+
41+
42+
Follow up:
43+
44+
Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree?<br>
3045

3146
## Idea
3247

0 commit comments

Comments
 (0)