Skip to content

Commit fe22e78

Browse files
committed
22/12/2020
1 parent 439c321 commit fe22e78

File tree

3 files changed

+75
-60
lines changed

3 files changed

+75
-60
lines changed

Daily-challenge/Dec/21/README.md

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
# class Solution(object):
2-
def decodeAtIndex(self, S, K):
3-
size = 0
4-
# Find size = length of decoded string
5-
for c in S:
6-
if c.isdigit():
7-
size *= int(c)
8-
else:
9-
size += 1
10-
11-
for c in reversed(S):
12-
K %= size
13-
if K == 0 and c.isalpha():
14-
return c
15-
16-
if c.isdigit():
17-
size /= int(c)
18-
else:
19-
size -= 1<br>
1+
# Smallest Range II
202

3+
Solution
4+
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once).
5+
6+
After this process, we have some array B.
7+
8+
Return the smallest possible difference between the maximum value of B and the minimum value of B.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = [1], K = 0
15+
Output: 0
16+
Explanation: B = [1]
17+
Example 2:
18+
19+
Input: A = [0,10], K = 2
20+
Output: 6
21+
Explanation: B = [2,8]
22+
Example 3:
23+
24+
Input: A = [1,3,6], K = 3
25+
Output: 3
26+
Explanation: B = [4,6,3]
27+
28+
29+
Note:
30+
31+
1 <= A.length <= 10000
32+
0 <= A[i] <= 10000
33+
0 <= K <= 10000
2134
## Idea
2235
Just do while loop and convert to decimal
2336

2437
## Code
2538
```python
2639
class Solution:
27-
def atMostNGivenDigitSet(self, D: List[str], N: int) -> int:
28-
N = str(N)
29-
n = len(N)
30-
res = sum(len(D) ** i for i in range(1, n))
31-
i = 0
32-
while i < len(N):
33-
res += sum(c < N[i] for c in D) * (len(D) ** (n - i - 1))
34-
if N[i] not in D: break
35-
i += 1
36-
return res + (i == n)
40+
def smallestRangeII(self, A, K):
41+
A.sort()
42+
res = A[-1] - A[0]
43+
44+
for i in range(len(A) - 1):
45+
big = max(A[-1], A[i] + 2 * K)
46+
small = min(A[i+1], A[0] + 2 * K)
47+
res = min(res, big - small)
48+
return res
49+
3750
```

Daily-challenge/Dec/21/sol.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
2-
def atMostNGivenDigitSet(self, D: List[str], N: int) -> int:
3-
N = str(N)
4-
n = len(N)
5-
res = sum(len(D) ** i for i in range(1, n))
6-
i = 0
7-
while i < len(N):
8-
res += sum(c < N[i] for c in D) * (len(D) ** (n - i - 1))
9-
if N[i] not in D: break
10-
i += 1
11-
return res + (i == n)
2+
def smallestRangeII(self, A, K):
3+
A.sort()
4+
res = A[-1] - A[0]
5+
6+
for i in range(len(A) - 1):
7+
big = max(A[-1], A[i] + 2 * K)
8+
small = min(A[i+1], A[0] + 2 * K)
9+
res = min(res, big - small)
10+
return res
11+

Daily-challenge/Dec/22/README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
# Unique Morse Code Words
2-
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
1+
# Balanced Binary Tree
2+
Given a binary tree, determine if it is height-balanced.
33

4-
For convenience, the full table for the 26 letters of the English alphabet is given below:
4+
For this problem, a height-balanced binary tree is defined as:
55

6-
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
7-
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-..--...", (which is the concatenation "-.-." + ".-" + "-..."). We'll call such a concatenation, the transformation of a word.
6+
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
87

9-
Return the number of different transformations among all words we have.
8+
109

11-
Example:
12-
Input: words = ["gin", "zen", "gig", "msg"]
13-
Output: 2
14-
Explanation:
15-
The transformation of each word is:
16-
"gin" -> "--...-."
17-
"zen" -> "--...-."
18-
"gig" -> "--...--."
19-
"msg" -> "--...--."
10+
Example 1:
2011

21-
There are 2 different transformations, "--...-." and "--...--.".
22-
Note:
2312

24-
The length of words will be at most 100.
25-
Each words[i] will have le
26-
gth in range [1, 12].
27-
words[i] will only consist of lowercase letters.<br>
13+
Input: root = [3,9,20,null,null,15,7]
14+
Output: true
15+
Example 2:
16+
17+
18+
Input: root = [1,2,2,3,3,null,null,4,4]
19+
Output: false
20+
Example 3:
21+
22+
Input: root = []
23+
Output: true
24+
25+
26+
Constraints:
27+
28+
The number of nodes in the tree is in the range [0, 5000].
29+
-104 <= Node.val <= 104<br>
2830

2931
## Idea
3032
Just do while loop and convert to decimal

0 commit comments

Comments
 (0)