Skip to content

Commit d2112bb

Browse files
committed
03/12/2020
1 parent d351c3d commit d2112bb

File tree

6 files changed

+109
-123
lines changed

6 files changed

+109
-123
lines changed

Daily-challenge/Dec/01/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ Just do while loop and convert to decimal
3434

3535
## Code
3636
```python
37-
# Definition for singly-linked list.
38-
# class ListNode:
39-
# def __init__(self, val=0, next=None):
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, val=0, left=None, right=None):
4040
# self.val = val
41-
# self.next = next
41+
# self.left = left
42+
# self.right = right
4243
class Solution:
43-
def getDecimalValue(self, head: ListNode) -> int:
44-
string = ""
45-
while head:
46-
string += str(head.val)
47-
head = head.next
48-
# print(string)
49-
return int(string, 2)
44+
def maxDepth(self, root: TreeNode) -> int:
45+
if not root:
46+
return 0
47+
48+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
5049
```

Daily-challenge/Dec/01/sol.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
44
# self.val = val
5-
# self.next = next
5+
# self.left = left
6+
# self.right = right
67
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)
8+
def maxDepth(self, root: TreeNode) -> int:
9+
if not root:
10+
return 0
11+
12+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

Daily-challenge/Dec/02/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,29 @@ two ways, one is to use a list and cheat, or we can use standard while loop and
2525
# self.val = val
2626
# self.next = next
2727
class Solution:
28-
def insertionSortList(self, head: ListNode) -> ListNode:
29-
dum = ListNode(0)
30-
prev = dum
31-
28+
29+
def __init__(self, head: ListNode):
30+
"""
31+
@param head The linked list's head.
32+
Note that the head is guaranteed to be not null, so it contains at least one node.
33+
"""
34+
self.range = []
3235
while head:
33-
temp = head.next
34-
if prev.val >= head.val:
35-
prev = dum
36-
while prev.next and prev.next.val < head.val:
37-
prev = prev.next
38-
39-
head.next = prev.next
40-
prev.next = head
41-
head = temp
42-
return dum.next
36+
self.range.append(head.val)
37+
head = head.next
38+
39+
40+
def getRandom(self) -> int:
41+
"""
42+
Returns a random node's value.
43+
"""
44+
num = int(random.random() * len(self.range))
45+
return self.range[num]
46+
47+
48+
49+
# Your Solution object will be instantiated and called as such:
50+
# obj = Solution(head)
51+
# param_1 = obj.getRandom()
4352

4453
```

Daily-challenge/Dec/02/sol.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,27 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def insertionSortList(self, head: ListNode) -> ListNode:
8-
mylist=[]
9-
ptr=head
10-
while ptr:
11-
mylist.append(ptr.val)
12-
ptr=ptr.next
13-
14-
mylist.sort()
15-
16-
ptr=head
17-
for val in mylist:
18-
ptr.val=val
19-
ptr=ptr.next
7+
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
2017

21-
return head
2218

23-
# Definition for singly-linked list.
24-
class ListNode:
25-
def __init__(self, val=0, next=None):
26-
self.val = val
27-
self.next = next
28-
class Solution:
29-
def insertionSortList(self, head: ListNode) -> ListNode:
30-
dum = ListNode(0)
31-
prev = dum
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]
3225

33-
while head:
34-
temp = head.next
35-
if prev.val >= head.val:
36-
prev = dum
37-
while prev.next and prev.next.val < head.val:
38-
prev = prev.next
39-
40-
head.next = prev.next
41-
prev.next = head
42-
head = temp
43-
return dum.next
44-
26+
27+
28+
# Your Solution object will be instantiated and called as such:
29+
# obj = Solution(head)
30+
# param_1 = obj.getRandom()

Daily-challenge/Dec/03/README.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,48 @@
1-
# Consecutive Characters
2-
3-
Solution
4-
Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.
5-
6-
Return the power of the string.
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.
73

84

95

106
Example 1:
117

12-
Input: s = "leetcode"
13-
Output: 2
14-
Explanation: The substring "ee" is of length 2 with the character 'e' only.
15-
Example 2:
168

17-
Input: s = "abbcccddddeeeeedcba"
18-
Output: 5
19-
Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
20-
Example 3:
21-
22-
Input: s = "triplepillooooow"
23-
Output: 5
24-
Example 4:
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]
11+
Example 2:
2512

26-
Input: s = "hooraaaaaaaaaaay"
27-
Output: 11
28-
Example 5:
2913

30-
Input: s = "tourist"
31-
Output: 1
14+
Input: root = [5,1,7]
15+
Output: [1,null,5,null,7]
3216

3317

3418
Constraints:
3519

36-
1 <= s.length <= 500
37-
s contains only lowercase English letters. <br>
20+
The number of nodes in the given tree will be in the range [1, 100].
21+
0 <= Node.val <= 1000 <br>
3822

3923
## Idea
4024
Just a while loop or for loop is fine
4125

4226
## Code
4327

4428
```python
45-
class Solution:
46-
def maxPower(self, s: str) -> int:
47-
i = 1
48-
n = len(s)
49-
count = 1
50-
res = 1
51-
while i < n:
52-
if s[i] == s[i-1]:
53-
count += 1
54-
res = max(res, count)
55-
else:
56-
# res = max(res, count)
57-
count = 1
58-
i += 1
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
5947
return res
6048
```

Daily-challenge/Dec/03/sol.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
class Solution:
2-
def maxPower(self, s: str) -> int:
3-
i = 1
4-
n = len(s)
5-
count = 1
6-
res = 1
7-
while i < n:
8-
if s[i] == s[i-1]:
9-
count += 1
10-
res = max(res, count)
11-
else:
12-
# res = max(res, count)
13-
count = 1
14-
i += 1
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
1519
return res

0 commit comments

Comments
 (0)