Skip to content

Commit 758ba68

Browse files
author
wuduhren
committed
Updates
1 parent 543e1f8 commit 758ba68

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

problems/python3/design-twitter.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Twitter:
2+
3+
def __init__(self):
4+
self.followData = collections.defaultdict(set)
5+
self.tweetData = collections.defaultdict(list)
6+
self.count = 0
7+
8+
9+
def postTweet(self, userId: int, tweetId: int) -> None:
10+
self.tweetData[userId].append((self.count, tweetId))
11+
self.count -= 1
12+
13+
14+
def getNewsFeed(self, userId: int) -> List[int]:
15+
newsFeed = []
16+
h = []
17+
18+
self.followData[userId].add(userId)
19+
for followeeId in self.followData[userId]:
20+
if followeeId not in self.tweetData: continue
21+
index = len(self.tweetData[followeeId])-1
22+
count, tweetId = self.tweetData[followeeId][index]
23+
h.append((count, tweetId, followeeId, index-1))
24+
heapq.heapify(h)
25+
26+
while h and len(newsFeed)<10:
27+
_, tweetId, userId, index = heapq.heappop(h)
28+
newsFeed.append(tweetId)
29+
if index>=0:
30+
count, tweetId2 = self.tweetData[userId][index]
31+
heapq.heappush(h, (count, tweetId2, userId, index-1))
32+
return newsFeed
33+
34+
35+
36+
def follow(self, followerId: int, followeeId: int) -> None:
37+
self.followData[followerId].add(followeeId)
38+
39+
def unfollow(self, followerId: int, followeeId: int) -> None:
40+
if followerId not in self.followData: return
41+
self.followData[followerId].remove(followeeId)
42+
43+
44+
# Your Twitter object will be instantiated and called as such:
45+
# obj = Twitter()
46+
# obj.postTweet(userId,tweetId)
47+
# param_2 = obj.getNewsFeed(userId)
48+
# obj.follow(followerId,followeeId)
49+
# obj.unfollow(followerId,followeeId)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class MedianFinder:
2+
3+
def __init__(self):
4+
self.large = [] #store nums larger or equal to the median
5+
self.small = [] #store nums samaller to the median
6+
7+
def addNum(self, num: int) -> None:
8+
if not self.large and not self.small:
9+
heapq.heappush(self.large, num)
10+
elif num>=self.findMedian():
11+
heapq.heappush(self.large, num)
12+
self.balance()
13+
else:
14+
heapq.heappush(self.small, -num)
15+
self.balance()
16+
17+
def balance(self) -> None:
18+
#make the length of two heaps as even as posible
19+
if len(self.large)>len(self.small)+1:
20+
num = heapq.heappop(self.large)
21+
heapq.heappush(self.small, -num)
22+
23+
if len(self.small)>len(self.large):
24+
num = -heapq.heappop(self.small)
25+
heapq.heappush(self.large, num)
26+
27+
28+
def findMedian(self) -> float:
29+
if (len(self.large)+len(self.small))%2==0:
30+
return (self.large[0]-self.small[0])/2
31+
else:
32+
return self.large[0]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
3+
h = []
4+
5+
for x, y in points:
6+
dis = (x**2+y**2)**0.5
7+
heapq.heappush(h, (-dis, x, y))
8+
if len(h)>k: heapq.heappop(h)
9+
10+
return [(x, y) for dis, x, y in h]

problems/python3/task-scheduler.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def leastInterval(self, tasks: List[str], n: int) -> int:
3+
q = collections.deque()
4+
h = []
5+
time = 0
6+
7+
counter = collections.Counter(tasks)
8+
for task in counter:
9+
heapq.heappush(h, (-counter[task], task))
10+
11+
while h or q:
12+
if q and q[0][0]<=time:
13+
_, count, task = q.popleft()
14+
heapq.heappush(h, (-count, task))
15+
16+
if h:
17+
count, task = heapq.heappop(h)
18+
count*=-1
19+
count -= 1
20+
if count>0: q.append((time+n+1, count, task))
21+
time += 1
22+
23+
return time

0 commit comments

Comments
 (0)