8.15 ~ 8.21 尽量坚持刷力扣的每日一题,锻炼大脑思维。更新中~~
设置一个数组用来模拟循环队列,只需要对队头和队尾取模即可。front指的是队头,rear指队尾后一个,这样可以避免队列已满和队列为空冲突。
class MyCircularDeque:
def __init__(self, k: int):
self.front = self.rear = 0
self.arr = [0] * (k + 1)
def insertFront(self, value: int) -> bool:
if self.isFull():
return False
self.front = (self.front - 1) % len(self.arr)
self.arr[self.front] = value
return True
def insertLast(self, value: int) -> bool:
if self.isFull():
return False
self.arr[self.rear] = value
self.rear = (self.rear + 1) % len(self.arr)
return True
def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front + 1) % len(self.arr)
return True
def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.rear = (self.rear - 1) % len(self.arr)
return True
def getFront(self) -> int:
if self.isEmpty():
return -1
return self.arr[self.front]
def getRear(self) -> int:
if self.isEmpty():
return -1
return self.arr[(self.rear - 1) % len(self.arr)]
def isEmpty(self) -> bool:
if self.rear == self.front:
return True
return False
def isFull(self) -> bool:
if (self.rear + 1) % len(self.arr) == self.front:
return True
return False
根据题意模拟即可
class OrderedStream:
def __init__(self, n: int):
self.stream = [""] * n
self.ptr = 0
def insert(self, idKey: int, value: str) -> List[str]:
stream1 = self.stream
stream1[idKey - 1] = value
res = list()
while self.ptr < len(stream1) and stream1[self.ptr]:
res.append(stream1[self.ptr])
self.ptr += 1
return res
用队列,记录最后一层的数组和
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
queue, ans = deque([root]), 0
while queue:
cur = 0
for i in range(len(queue)):
node = queue.popleft()
cur += node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
ans = cur
return ans
首先用cnt表示每个数字出现的次数,sums表示出现相同次数的有多少个数字,mx表示当前最大的出现次数,cur表示当前数字出现的次数。
本题满足条件的只有三个条件:
class Solution:
def maxEqualFreq(self, nums: List[int]) -> int:
cnt = [0] * (100005)
sums= [0] * (100005)
mx, ans = 0, 0
for i, num in enumerate(nums):
cnt[num] += 1
cur = cnt[num]
length = i + 1
sums[cur] += 1
sums[cur - 1] -= 1 # 一个数字只能被sums记一次,就是说出现了4次,不能出现3次
mx = max(mx, cur)
if mx == 1:
ans = length
elif mx * sums[mx] + 1 == length:
ans = length
elif (mx - 1) * (sums[mx - 1] + 1) + 1 == length:
ans = length
return ans
本题数据量小,简单遍历即可。
class Solution:
def busyStudent(self, st: List[int], et: List[int], qt: int) -> int:
ans = 0
for s, e in zip(st, et):
if s <= qt <= e:
ans += 1
return ans
如果本题数据量大一些,比如startTime和endTime大一点,以及queryTime数据量多一些,就需要用到差分思想。
class Solution:
def busyStudent(self, st: List[int], et: List[int], qt: int) -> int:
maxTime = max(et)
if qt > maxTime:
return 0
cnt = [0] * (maxTime + 2)
for s, e in zip(st, et):
cnt[s] += 1
cnt[e + 1] -= 1
return sum(cnt[:qt + 1])
先找集合中最大的数,以这个数为根节点,然后再进行这个数的左边和右边分别递归,做根节点的左右子树。
class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
def dfs(left: int, right: int) -> Optional[TreeNode]:
if right < left:
return None
tmp = left
for i in range(left + 1, right + 1):
if nums[i] > nums[tmp]:
tmp = i
node = TreeNode(nums[tmp])
node.left = dfs(left, tmp - 1)
node.right = dfs(tmp + 1, right)
return node
return dfs(0, len(nums) - 1)
如果有存在单词前缀,那就是前面空格数量+1,只需要遍历空格后的单词以及第一个单词找前缀,看是否满足即可。
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
ans = -1
n = len(sentence)
k = 0
for i in range(n):
if sentence[i] == " ":
k += 1
if (i != 0 and sentence[i - 1] != " "):
continue
j, t = 0, i
m = len(searchWord)
while t < n and j < m and sentence[t] == searchWord[j]:
j += 1
t += 1
if (j == m):
ans = k + 1
break
return ans