# A. Find Resultant Array After Removing Anagrams (opens new window)
Remove the words having duplicated char set.
DETAILS
def removeAnagrams(self, W: List[str]) -> List[str]:
n = len(W)
removed = []
for i in range(1, n):
if sorted(list(W[i])) == sorted(list(W[i - 1])):
removed.append(i)
ans = []
for i, w in enumerate(W):
if i not in removed:
ans.append(w)
return ans
# B. Maximum Consecutive Floors Without Special Floors (opens new window)
Get the largest gap.
DETAILS
int maxConsecutive(int bot, int top, vector<int>& A) {
sort(A.begin(), A.end());
int ans = max(A[0] - bot, top - A.back());
if (A.size() < 2)
return ans;
for (int i = 1; i < A.size(); ++i)
ans = max(ans, A[i] - A[i - 1] - 1);
return ans;
}
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
if len(special) <= 1:
return max(special[0] - bottom, top - special[0])
special.sort()
ans=max([a - b - 1 for a, b in zip(special[1:], special[:-1])])
return max(ans, special[0] - bottom, top - special[-1])
# C. Largest Combination With Bitwise AND Greater Than Zero (opens new window)
Just count the digits of every number in their binary representations. For example, if 3 numbers (5-101, 7-111, 4-100) having 1
on the 3rd digit, meaning AND
of these numbers is non-zero.
DETAILS
int largestCombination(vector<int>& A) {
vector<int> counter(25, 0);
for (auto a : A)
for (int i = 0; i < 25; ++i) {
counter[i] += a & 1;
a >>= 1;
}
return *max_element(counter.begin(), counter.end());
}
# D. Count Integers in Intervals (opens new window)
Sorted list.
DETAILS
from sortedcontainers import SortedList
class CountIntervals:
def __init__(self):
self.ss = 0
self.lft = SortedList()
self.rgt = SortedList()
def add(self, left: int, right: int) -> None:
idx = self.rgt.bisect_left(left)
jdx = self.lft.bisect_right(right)
if idx == jdx:
self.ss += 1 + right - left
self.lft.add(left)
self.rgt.add(right)
else:
prev = 0
lmst = min(left, self.lft[idx])
rmst = max(right, self.rgt[jdx - 1])
for k in range(idx, jdx):
cr = self.rgt.pop(idx)
cl = self.lft.pop(idx)
prev += 1 + cr - cl
rmst = max(cr, rmst)
lmst = min(cl, lmst)
self.lft.add(lmst)
self.rgt.add(rmst)
self.ss += (1 + rmst - lmst)- prev
def count(self) -> int:
return self.ss