Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01a7cdf

Browse files
Initial commit
1 parent 96daf25 commit 01a7cdf

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎distribute_bonuses_pro.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def getBonuses(performances):
2+
count = len(performances)
3+
bonuses = [1] * count
4+
5+
for i in range(1, count):
6+
if performances[i - 1] < performances[i]:
7+
bonuses[i] = bonuses[i - 1] + 1
8+
9+
for i in range(count - 2, -1, -1):
10+
if performances[i + 1] < performances[i]:
11+
bonuses[i] = max(bonuses[i], bonuses[i + 1] + 1)
12+
13+
return bonuses
14+
15+
print(getBonuses([1, 2, 3, 4, 3, 1]))
16+
# [1, 2, 3, 4, 2, 1]

‎running_median_pro.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import heapq
2+
3+
def add(num, min_heap, max_heap):
4+
if len(min_heap) + len(max_heap) <= 1:
5+
heapq.heappush(max_heap, -num)
6+
return
7+
8+
median = get_median(min_heap, max_heap)
9+
if num > median:
10+
heapq.heappush(min_heap, num)
11+
else:
12+
heapq.heappush(max_heap, -num)
13+
14+
def rebalance(min_heap, max_heap):
15+
if len(min_heap) > len(max_heap) + 1:
16+
root = heapq.heappop(min_heap)
17+
heapq.heappush(max_heap, -root)
18+
elif len(max_heap) > len(min_heap) + 1:
19+
root = -heapq.heappop(max_heap)
20+
heapq.heappush(min_heap, root)
21+
22+
def print_median(min_heap, max_heap):
23+
print(get_median(min_heap, max_heap))
24+
25+
26+
def get_median(min_heap, max_heap):
27+
if len(min_heap) > len(max_heap):
28+
return min_heap[0]
29+
elif len(min_heap) < len(max_heap):
30+
return -max_heap[0]
31+
else:
32+
return (min_heap[0] + -max_heap[0]) / 2.0
33+
34+
35+
def running_median(stream):
36+
min_heap = []
37+
max_heap = []
38+
answer = []
39+
for num in stream:
40+
add(num, min_heap, max_heap)
41+
rebalance(min_heap, max_heap)
42+
answer.append(get_median(min_heap, max_heap))
43+
return answer
44+
45+
def running_median2(stream):
46+
heapl, heapr = [], []
47+
res = []
48+
for num in stream:
49+
heapq.heappush(heapl, num)
50+
if len(heapl)-len(heapr) > 1:
51+
heapq.heappush(heapr, -(heapq.heappop(heapl)))
52+
if len(heapl) > len(heapr):
53+
res.append(heapl[0])
54+
else:
55+
res.append((heapl[0]-heapr[0])/2)
56+
return res
57+
58+
print(running_median([2, 1, 4, 7, 2, 0, 5]))
59+
# [2, 1.5, 2, 3, 2, 2, 2]
60+
61+
print(running_median2([2, 1, 4, 7, 2, 0, 5]))
62+
# [2, 1.5, 2, 3, 2, 2, 2]

‎word_concatenation_pro.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def findAllConcatenatedWords(self, words):
3+
wordDict = set(words)
4+
cache = {}
5+
return [word for word in words if self._canForm(word, wordDict, cache)]
6+
7+
def _canForm(self, word, wordDict, cache):
8+
if word in cache:
9+
return cache[word]
10+
11+
for index in range(1, len(word)):
12+
prefix = word[:index]
13+
suffix = word[index:]
14+
if prefix in wordDict:
15+
if suffix in wordDict or self._canForm(suffix, wordDict, cache):
16+
cache[word] = True
17+
return True
18+
cache[word] = False
19+
return False
20+
21+
input = ['cat', 'cats', 'dog', 'catsdog']
22+
print(Solution().findAllConcatenatedWords(input))
23+
# ['catsdog']

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /