Skip to content

Commit 670a364

Browse files
committedJun 22, 2020
2020-06-21
1 parent 2194e4d commit 670a364

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed
 
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def simplifiedFractions(self, n: int) -> List[str]:
33
import math
4-
res = set()
4+
res = []
55

66
for down in range(1, n + 1):
77
for up in range(1, down):
88
if math.gcd(up, down) == 1:
9-
res.add(str(up) + "/" + str(down))
9+
res.append(str(up) + "/" + str(down))
1010

11-
return list(res)
11+
return res
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def canBeEqual(self, target, arr):
3+
"""
4+
:type target: List[int]
5+
:type arr: List[int]
6+
:rtype: bool
7+
"""
8+
from collections import Counter
9+
return Counter(target) == Counter(arr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def hasAllCodes(self, s, k):
3+
"""
4+
:type s: str
5+
:type k: int
6+
:rtype: bool
7+
"""
8+
numSet = set()
9+
for i in range(len(s) - k + 1):
10+
numSet.add(int(s[i:i + k], 2))
11+
12+
for num in range(2 ** k):
13+
if num not in numSet:
14+
return False
15+
return True
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution(object):
2+
def checkIfPrerequisite(self, n, prerequisites, queries):
3+
"""
4+
:type n: int
5+
:type prerequisites: List[List[int]]
6+
:type queries: List[List[int]]
7+
:rtype: List[bool]
8+
"""
9+
from collections import defaultdict
10+
from heapq import *
11+
pre = defaultdict(set)
12+
children = defaultdict(set)
13+
inDegree = defaultdict(int)
14+
15+
for src, dec in prerequisites:
16+
inDegree[dec] += 1
17+
children[src].add(dec)
18+
19+
queue = []
20+
for i in range(n):
21+
if inDegree[i] == 0:
22+
heappush(queue, i)
23+
24+
while queue:
25+
cur = heappop(queue)
26+
27+
for child in children[cur]:
28+
pre[child] = pre[cur] | set([cur]) | pre[child]
29+
30+
inDegree[child] -= 1
31+
if inDegree[child] == 0:
32+
heappush(queue, child)
33+
34+
res = []
35+
for src, dec in queries:
36+
res.append(src in pre[dec])
37+
return res

0 commit comments

Comments
 (0)