Skip to content

Commit 383dbc1

Browse files
committed
Update ./topic to latest
1 parent 388ab21 commit 383dbc1

File tree

7 files changed

+98
-34
lines changed

7 files changed

+98
-34
lines changed

topic/bit_manipulation/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ output 1 if input 0
2929
>>> 0 ^ 1
3030
1
3131
```
32+
33+
## To check a number is power of 2
34+
35+
if a number is power of 2, then there is only 1 in the bit format in the number.
36+
37+
```python
38+
>>> [(num & (num - 1)) == 0 for num in (4, 5, 11, 16, 21, 29, 32)]
39+
[True, False, False, True, False, False, True]
40+
```

topic/complexity_analysis.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ for (int i = N; i > 0; i /= 2) {
2121

2222
If X will always be a better choice for **large inputs**,
2323
then we say that **an algorithm X is asymptotically more efficient than Y**.
24+
25+
## Case #3
26+
27+
- time: `O(N)`
28+
- the `j` go through that array only once.
29+
30+
```java
31+
int j = 0;
32+
for(int i = 0; i < n; ++i) {
33+
while(j < n && arr[i] < arr[j]) {
34+
j++;
35+
}
36+
}
37+
```
38+
39+
## Case #4
40+
41+
- time: `O(2 ^ N)`
42+
- REF: https://math.stackexchange.com/questions/177405/prove-by-induction-2n-cn-0-cn-1-cdots-cn-n
43+
44+
`C(n, 0) + C(n, 1) + ... + C(n, n) = 2 ^ n`

topic/hash/python/geohash.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
"""
2-
main concept for encoding
3-
4-
1. use binary search to convert `lng` and `lat` to `bin_code`
5-
`1`: up or right
6-
`0`: down or left
7-
2. merge `lng_codes` and `lat_codes` alternately into one `bin_codes`
8-
`lng_codes[0] + lat_codes[0] + lng_codes[1] + ...`
9-
3. pick every 5 digit from `bin_codes` and convert that to `base32`
2+
REF:
3+
4+
- lintcode/529_geohash.py
5+
- lintcode/530_geohash_ii.py
6+
7+
8+
Main Concept: for encoding
9+
10+
1. use binary search to convert `lng` and `lat` to `bincode`
11+
1: up or right
12+
0: down or left
13+
2. merge `lngcode` and `latcode` alternately into one `bincode`
14+
`lngcode[0] + latcode[0] + lngcode[1] + ...`
15+
3. pick every 5 digit from `bincode` and convert that to base32 chars
1016
"""
1117

1218

topic/heap/python/binary_hash_heap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def _siftup(self, i):
9292
while i * 2 <= size:
9393
j = i * 2
9494

95-
if i * 2 + 1 <= size and heap[i * 2 + 1] < heap[i * 2]:
96-
j = i * 2 + 1
95+
if j + 1 <= size and heap[j + 1] < heap[j]:
96+
j += 1
9797

9898
if heap[i] > heap[j]:
9999
self._swap(i, j)

topic/heap/python/binary_heap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def _siftup(self, i):
6868
while i * 2 <= size:
6969
j = i * 2
7070

71-
if i * 2 + 1 <= size and heap[i * 2 + 1] < heap[i * 2]:
72-
j = i * 2 + 1
71+
if j + 1 <= size and heap[j + 1] < heap[j]:
72+
j += 1
7373

7474
if heap[i] > heap[j]:
7575
heap[i], heap[j] = heap[j], heap[i]

topic/problem_set/subarray_sum.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Differences
2+
3+
`leetcode/209_minimum_size_subarray_sum.py`
4+
5+
- elements is non-negative
6+
- sum >= k
7+
- find min length
8+
9+
`leetcode/325_maximum_size_subarray_sum_equals_k.py`
10+
11+
- elements may be negative
12+
- sum == k
13+
- find max length
14+
15+
`leetcode/560_subarray_sum_equals_k.py`
16+
17+
- elements may be negative
18+
- sum == k
19+
- count how many subarrays
20+
21+
`leetcode/862_shortest_subarray_with_sum_at_least_k.py`
22+
23+
- elements may be negative
24+
- sum >= k
25+
- find min length
26+
27+
## Solutions
28+
29+
- Non-negative => Two Pointers
30+
- Negative => Prefix Sum

topic/tree/python/binary_tree.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,64 @@ def __init__(self, val):
66

77
class BinaryTree:
88
EMPTY = '#'
9+
TMPL = '{{{}}}' # {{, }} is to escape brackets
910

1011
@classmethod
1112
def serialize(cls, root):
1213
"""Encodes a tree to a single string.
1314
:type root: TreeNode
1415
:rtype: str
1516
"""
16-
TEMPLATE = '{{{}}}' # {{, }} is to escape brackets
1717
if not root:
18-
return TEMPLATE.format('')
18+
return cls.TMPL.format('')
1919

20-
vals = []
20+
values = []
2121
queue = [root]
2222

2323
for node in queue:
2424
if not node:
25-
vals.append(cls.EMPTY)
25+
values.append(cls.EMPTY)
2626
continue
2727

28-
vals.append(str(node.val))
28+
values.append(str(node.val))
2929
queue.append(node.left)
3030
queue.append(node.right)
3131

32-
while vals[-1] == cls.EMPTY:
33-
vals.pop()
32+
while values[-1] == cls.EMPTY:
33+
values.pop()
3434

35-
return TEMPLATE.format(','.join(vals))
35+
return cls.TMPL.format(','.join(values))
3636

3737
@classmethod
3838
def deserialize(cls, data):
3939
"""Decodes your encoded data to tree.
4040
:type data: str
4141
:rtype: TreeNode
4242
"""
43-
if (
44-
not data or
45-
data[0] != '{' or
46-
data[-1] != '}' or
47-
len(data) < 3 or
48-
data[1] == cls.EMPTY
49-
):
43+
if any((
44+
not data,
45+
len(data) < 3,
46+
data[0] != '{',
47+
data[-1] != '}',
48+
data[1] in (cls.EMPTY, ','),
49+
)):
5050
return
5151

52-
vals = data[1:-1].split(',')
53-
n = len(vals)
52+
values = data[1:-1].split(',')
5453
i = 0
55-
56-
root = TreeNode(int(vals[i]))
54+
root = TreeNode(int(values[i]))
5755
queue = [root]
5856

5957
for node in queue:
6058
for branch in ('left', 'right'):
6159
i += 1
6260

63-
if i >= n:
61+
if i >= len(values):
6462
break
65-
if vals[i] == cls.EMPTY:
63+
if values[i] == cls.EMPTY:
6664
continue
6765

68-
setattr(node, branch, TreeNode(int(vals[i])))
66+
setattr(node, branch, TreeNode(int(values[i])))
6967
queue.append(getattr(node, branch))
7068

7169
return root

0 commit comments

Comments
 (0)