更多
第 293 场周赛
class Solution:
def removeAnagrams(self, words: List[str]) -> List[str]:
q = [words[0]]
for w in words:
if sorted(q[-1]) != sorted(w):
q.append(w)
return q
class Solution:
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
special.sort()
pre = bottom
ans = 0
for p in special:
ans = max(ans, p - pre)
pre = p + 1
ans = max(ans, top - pre + 1)
return ans
floors = [bottom - 1] + sorted(special) + [top + 1]
return max(u - d - 1 for u, d in zip(floors[1:], floors[:-1]))
两个数按位与大于 0,那么一定存在某一位都为 1。 统计 32 位上哪一位出现的个数最多,那么这一位是 1 的数 按位与 则大于 0。
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
ans = 0
for i in range(30):
cnt = 0
for x in candidates:
if x & (1 << i) > 0:
cnt += 1
ans = max(ans, cnt)
return ans
return max(len([c for c in candidates if (c & (1 << i)) > 0]) for i in range(30))
from sortedcontainers import SortedDict
class CountIntervals:
def __init__(self):
self.intervals = SortedDict()
self.cnt = 0
def add(self, left: int, right: int) -> None:
n = len(self.intervals)
i = self.intervals.bisect_left(left)
while i < n and self.intervals.values()[i] <= right:
r, l = self.intervals.items()[i]
left = min(left, l)
right = max(right, r)
self.intervals.popitem(i)
self.cnt -= r-l+1
n -= 1
self.intervals[right] = left
self.cnt+=right-left+1
def count(self) -> int:
return self.cnt
|