# A. Intersection of Multiple Arrays (opens new window)
Count the frequency of every number, return the sorted list of all the numbers that have frequency equals nums
length.
DETAILS
def intersection(self, A: List[List[int]]) -> List[int]:
freq = collections.defaultdict(int)
for a in A:
for n in a:
freq[n] += 1
ans = [x for x in freq if freq[x]==len(A)]
return sorted(ans)
# B. Count Lattice Points Inside a Circle (opens new window)
Count the number of lattice point in one quarter of the cirle.
DETAILS
def countLatticePoints(self, A: List[List[int]]) -> int:
ans = set()
for x,y,r in A:
for cx in range(x + 1, x + r + 1):
for cy in range(y + 1, y + r + 1):
if (cx - x) ** 2 + (cy - y) ** 2 <= r ** 2:
ans.add((cx, cy))
ans.add((2 * x - cx, cy))
ans.add((2 * x - cx, 2 * y - cy))
ans.add((cx, 2 * y - cy))
for cy in range(y - r, y + r + 1): ans.add((x, cy))
for cx in range(x - r, x + r + 1): ans.add((cx, y))
return len(ans)
# C. Count Number of Rectangles Containing Each Point (opens new window)
Binary search.
DETAILS
def countRectangles(self, A: List[List[int]], p: List[List[int]]) -> List[int]:
tmp=[[] for _ in range(100)]
ans = []
for x, y in A:
tmp[y-1].append(x)
for i in range(100): tmp[i].sort()
for x,y in p:
cur=0
for cury in range(y-1,100):
curx = bisect.bisect_left(tmp[cury],x)
cur+=len(tmp[cury])-curx
ans.append(cur)
return ans
# D. Number of Flowers in Full Bloom (opens new window)
Binary Search.
DETAILS
def fullBloomFlowers(self, A: List[List[int]], p: List[int]) -> List[int]:
positions = set()
for start, end in A:
positions.add(start)
positions.add(end + 1)
positions = sorted(list(positions))
idx = {pos: i for i, pos in enumerate(positions)}
tmp = [0] * (1 + len(positions))
for start, end in A:
tmp[idx[start]] += 1
tmp[idx[end + 1]] -= 1
presum = [0] + list(itertools.accumulate(tmp))
ans = []
for pp in p:
i = bisect.bisect_right(positions, pp)
ans.append(presum[i])
return ans