更多资源与服务
想要了解更多编程面试技巧,或者需要专业的面试辅导、OA代做、简历润色等服务?我们提供全方位的求职面试支持,帮助您在大厂面试真题、系统设计面试和算法面试中脱颖而出,轻松拿到心仪的 offer!无论您是留学生、刚踏入职场的新人,还是需要代码优化建议的开发者,我们的团队由ACM奖牌得主、大厂资深 TLM 及经验丰富的行业老兵组成,确保为您提供最专业的指导。
扫描下方二维码,添加我们的微信,获取更多服务:
关键词:
- 面试代面
- 代码代写
- OA代做
- 面试技巧
- 面试经验分享
- 职业规划
- 编程练习
让我们帮助您在技术面试中脱颖而出,实现职业上的飞跃!
Question 1: Counting Creator Communities
Description:
In a social network, there are multiple creators, and each one may be associated with others, forming a network community. Given an association matrix related
, where related[i][j] == '1'
indicates that creator i
and j
are associated (they belong to the same community), and related[i][j] == '0'
indicates no association.
Two creators are considered to be in the same community if they are directly or indirectly connected. For instance, if i
is connected to j
, and j
is connected to k
, then i
, j
, and k
belong to the same community.
Implement a function to count and return the total number of communities in the social network.
Input:
related
(List[List[str]]): Ann x n
matrix representing associations, whererelated[i][j]
is either'1'
or'0'
, andrelated[i][i]
is always'1'
.
Output:
- Returns an integer, the number of communities in the network.
Code Example:
class UF:
def __init__(self, size):
self.parent = list(range(size))
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def merge(self, x, y):
px, py = self.find(x), self.find(y)
self.parent[px] = py
def countCreatorCommunities(related):
n = len(related)
uf = UF(n)
for i in range(n):
for j in range(i+1, n):
if related[i][j] == '1':
uf.merge(i, j)
ds = set()
for i in range(n):
ds.add(uf.find(i))
return len(ds)
Question 2: Counting Good Arrays
Description:
Given an integer array arr
, where some elements are 0
, and other elements are positive integers, write a function to compute the number of “good arrays”. An array is considered a “good array” if it meets specific conditions, including:
- Elements in
arr
can be consecutive0
s or some non-zero integers. - The count of consecutive
0
s meeting specific conditions can generate valid subarrays.
The function should consider the position of these 0
s and how they interact with neighboring non-zero numbers to fulfill the “good array” definition.
Input:
arr
(List[int]): An array of integers with some elements as0
and others as positive integers.
Output:
- Returns the number of “good arrays” that meet the criteria, with the result modulo
10^9 + 7
.
Code Example:
def countGoodArrays(arr):
n = len(arr)
dp = [[0] * (n+1) for _ in range(n)]
mod = 10**9 + 7
dp[0][0] = 1
for i in range(1, n):
dp[i][0] = 2 * dp[i-1][1] + dp[i-1][0]
for j in range(1, i + 1):
dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1]
dp[i][j] %= mod
res = 1
sb = False
prev = -1
eb = False
c = 0
for i, x in enumerate(arr):
if x == 0:
c += 1
else:
eb = True
if not sb or not eb:
res *= 3**c
res %= mod
else:
if c > 0 and abs(x - arr[prev]) > c + 1:
return 0
res *= (dp[c][abs(x - arr[prev])] +
dp[c][abs(abs(x - arr[prev]) - 1)] +
dp[c][abs(x - arr[prev]) + 1])
res %= mod
sb = True
c = 0
prev = i
if c > 0:
res *= 3**c
res %= mod
return res