# A. Check if Every Row and Column Contains All Numbers (opens new window)
Check the number of unique numbers on each row and column.
DETAILS
def checkValid(self, A: List[List[int]]) -> bool:
return all(len(set(a)) == len(A) for a in A) and all(len(set(a)) == len(A) for a in zip(*A))
# 2134. Minimum Swaps to Group All 1's Together II (opens new window)
Sliding window.
DETAILS
def minSwaps(self, A: List[int]) -> int:
n = len(A)
num_one = sum(A)
num_zero = n - sum(A)
curr = sum(A[:num_one])
ans = num_one - curr
for i in range(num_one, n):
curr += A[i]
curr -= A[i - num_one]
ans = min(ans, num_one - curr)
curr = sum(A[:num_zero])
ans = min(ans, curr)
for i in range(num_zero, n):
curr += A[i]
curr -= A[i - num_zero]
ans = min(ans, curr)
return ans
# C. Count Words Obtained After Adding a Letter (opens new window)
Rearrange every word from S
into sorted anagram, for example: rumble -> belmru
.
Take a word t
from T
, remove one character from it and check if its sorted anagram appears in S
.
DETAILS
def wordCount(self, S: List[str], T: List[str]) -> int:
S = set(["".join(sorted(list(s))) for s in S])
ans = 0
for t in T:
cur_t = list(t)
for i in range(len(cur_t)):
tmp = cur_t[:i] + cur_t[i + 1:]
if "".join(sorted(tmp)) in S:
ans += 1
break
return ans
# D. Earliest Possible Day of Full Bloom (opens new window)
Plant the flower with shortest plantTime
.
DETAILS
def earliestFullBloom(self, P: List[int], G: List[int]) -> int:
res = 0
for grow, plant in sorted(zip(G, P)):
res = max(res, grow) + plant
return res