Skip to content

Commit 81e2730

Browse files
committed
24/12/2020
1 parent fe22e78 commit 81e2730

File tree

5 files changed

+91
-95
lines changed

5 files changed

+91
-95
lines changed

Daily-challenge/Dec/22/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ Just do while loop and convert to decimal
3333

3434
## Code
3535
```python
36+
# Definition for a binary tree node.
37+
# class TreeNode:
38+
# def __init__(self, val=0, left=None, right=None):
39+
# self.val = val
40+
# self.left = left
41+
# self.right = right
3642
class Solution:
37-
def uniqueMorseRepresentations(self, words: List[str]) -> int:
38-
lst = {"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."}
39-
final_lst = []
40-
for word in words:
41-
temp = ""
42-
for letter in word:
43-
temp += lst[letter]
44-
45-
final_lst += [temp]
46-
47-
res = len(set(final_lst))
48-
return res
43+
def isBalanced(self, root: TreeNode) -> bool:
44+
def dfs(root):
45+
if not root:
46+
return 0, True
47+
lefth, leftb = dfs(root.left)
48+
righth, rightb = dfs(root.right)
49+
return max(lefth, righth) + 1, abs(lefth - righth) <= 1 and leftb and rightb
50+
51+
return dfs(root)[1]
4952
```

Daily-challenge/Dec/22/sol.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
17
class Solution:
2-
def uniqueMorseRepresentations(self, words: List[str]) -> int:
3-
lst = {"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."}
4-
final_lst = []
5-
for word in words:
6-
temp = ""
7-
for letter in word:
8-
temp += lst[letter]
9-
10-
final_lst += [temp]
11-
12-
res = len(set(final_lst))
13-
return res
8+
def isBalanced(self, root: TreeNode) -> bool:
9+
def dfs(root):
10+
if not root:
11+
return 0, True
12+
lefth, leftb = dfs(root.left)
13+
righth, rightb = dfs(root.right)
14+
return max(lefth, righth) + 1, abs(lefth - righth) <= 1 and leftb and rightb
15+
16+
return dfs(root)[1]

Daily-challenge/Dec/23/README.md

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
1-
# House Robber III
2-
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
1+
# Next Greater Element III
32

4-
Determine the maximum amount of money the thief can rob tonight without alerting the police.
3+
Solution
4+
Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.
55

6-
Example 1:
6+
Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.
77

8-
Input: [3,2,3,null,3,null,1]
8+
99

10-
3
11-
/ \
12-
2 3
13-
\ \
14-
3 1
10+
Example 1:
1511

16-
Output: 7
17-
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
12+
Input: n = 12
13+
Output: 21
1814
Example 2:
1915

20-
Input: [3,4,5,1,3,null,1]
16+
Input: n = 21
17+
Output: -1
18+
2119

22-
3
23-
/ \
24-
4 5
25-
/ \ \
26-
1 3 1
20+
Constraints:
2721

28-
Output: 9
29-
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.<br>
22+
1 <= n <= 231 - 1<br>
3023

3124
## Idea
3225
Just do while loop and convert to decimal
3326

3427
## Code
3528
```python
36-
# Definition for a binary tree node.
37-
# class TreeNode:
38-
# def __init__(self, val=0, left=None, right=None):
39-
# self.val = val
40-
# self.left = left
41-
# self.right = right
4229
class Solution:
43-
def rob(self, root: TreeNode) -> int:
44-
if not root:
45-
return 0
46-
47-
def helper(node):
48-
if not node:
49-
return (0, 0)
50-
l1, l2 = helper(node.left)
51-
r1, r2 = helper(node.right)
52-
53-
return (node.val + l2 + r2, max(l1, l2) + max(r1, r2))
30+
def nextGreaterElement(self, n: int) -> int:
31+
digits = list(str(n))
32+
i = len(digits) - 1
33+
while i-1 >= 0 and digits[i] <= digits[i-1]:
34+
i -= 1
5435

55-
56-
return max(helper(root))
36+
if i == 0: return -1
37+
38+
j = i
39+
while j+1 < len(digits) and digits[j+1] > digits[i-1]:
40+
j += 1
41+
42+
digits[i-1], digits[j] = digits[j], digits[i-1]
43+
digits[i:] = digits[i:][::-1]
44+
ret = int(''.join(digits))
5745

46+
return ret if ret < 1<<31 else -1
5847
```

Daily-challenge/Dec/23/sol.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode:
3-
# def __init__(self, val=0, left=None, right=None):
4-
# self.val = val
5-
# self.left = left
6-
# self.right = right
71
class Solution:
8-
def rob(self, root: TreeNode) -> int:
9-
if not root:
10-
return 0
11-
12-
def helper(node):
13-
if not node:
14-
return (0, 0)
15-
l1, l2 = helper(node.left)
16-
r1, r2 = helper(node.right)
17-
18-
return (node.val + l2 + r2, max(l1, l2) + max(r1, r2))
19-
2+
def nextGreaterElement(self, n: int) -> int:
3+
digits = list(str(n))
4+
i = len(digits) - 1
5+
while i-1 >= 0 and digits[i] <= digits[i-1]:
6+
i -= 1
207

21-
return max(helper(root))
22-
8+
if i == 0: return -1
9+
10+
j = i
11+
while j+1 < len(digits) and digits[j+1] > digits[i-1]:
12+
j += 1
13+
14+
digits[i-1], digits[j] = digits[j], digits[i-1]
15+
digits[i:] = digits[i:][::-1]
16+
ret = int(''.join(digits))
17+
18+
return ret if ret < 1<<31 else -1

Daily-challenge/Dec/24/README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
# Basic Calculator II
2-
Implement a basic calculator to evaluate a simple expression string.
1+
# Swap Nodes in Pairs
2+
Given a linked list, swap every two adjacent nodes and return its head.
33

4-
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
4+
You may not modify the values in the list's nodes. Only nodes itself may be changed.
5+
6+
57

68
Example 1:
79

8-
Input: "3+2*2"
9-
Output: 7
10+
11+
Input: head = [1,2,3,4]
12+
Output: [2,1,4,3]
1013
Example 2:
1114

12-
Input: " 3/2 "
13-
Output: 1
15+
Input: head = []
16+
Output: []
1417
Example 3:
1518

16-
Input: " 3+5 / 2 "
17-
Output: 5
18-
Note:
19+
Input: head = [1]
20+
Output: [1]
21+
22+
23+
Constraints:
1924

20-
You may assume that the given expression is always valid.
21-
Do not use the eval built-in library function.<br>
25+
The number of nodes in the list is in the range [0, 100].
26+
0 <= Node.val <= 100.<br>
2227

2328
## Idea
2429
Just do while loop and convert to decimal

0 commit comments

Comments
 (0)