本文最后更新于 2 分钟前,文中所描述的信息可能已发生改变。
3731. 找出缺失的元素
题目类型
#枚举 #哈希表
解题思路
参考:这道题很简单!
示例代码
python
class Solution:
def findMissingElements(self, nums: List[int]) -> List[int]:
st = set(nums)
return [i for i in range(min(nums) + 1, max(nums)) if i not in st]3732. 一次替换后的三元素最大乘积
题目类型
#贪心
解题思路
参考:这道题很简单!
示例代码
python
class Solution:
def maxProduct(self, nums: List[int]) -> int:
a = sorted([abs(x) for x in nums])
return 10**5 * a[-1] * a[-2]3733. 完成所有送货任务的最少时间
题目类型
#二分 #数学 #容斥
解题思路
该题存在 做法。check假定两无人机之间的互斥充电时间穿插工作,同时剔除共同充电时间作为黑区,禁止两无人机使用。
示例代码
python
class Solution:
def minimumTime(self, ds: List[int], rs: List[int]) -> int:
d1, d2 = ds
r1, r2 = rs
lc = lcm(r1, r2)
l = d1 + d2
r = (d1 + d2) * 2 + 1
while l <= r:
m = l + r >> 1
c = m // lc
c1 = m // r1 - c
c2 = m // r2 - c
if max(0, d1 - c2) + max(0, d2 - c1) <= m - (c1 + c2 + c):
r = m - 1
else:
l = m + 1
return l3734. 大于目标字符串的最小字典序回文排列
题目类型
#贪心 #倒序贪心 #字典序
解题思路
首先判断 s 是否能构成回文字符串,能够满足只需要构造前半部分;从后往前枚举判断是否满足构造 t[0: i - 1]部分的字符串,满足则可以调控字典序。
注意特判,s 和 t 前半部分相同时的字典序大小,如 baab 和 baaa,无需调控已经满足题目条件。
示例代码
python
class Solution:
def lexPalindromicPermutation(self, s: str, target: str) -> str:
left = Counter(s)
odd = []
for c in ascii_lowercase:
if left[c] % 2:
odd.append(c)
left[c] //= 2
if len(odd) > 1:
return ""
n = len(s)
ans = list(target[: n // 2])
for c in ans:
left[c] -= 1
if all(cnt == 0 for cnt in left.values()):
t = "".join(ans + odd + ans[::-1])
if t > target:
return t
for i in range(len(ans) - 1, -1, -1):
c = ans[i]
left[c] += 1
if any(cnt < 0 for cnt in left.values()):
continue
for t in ascii_lowercase[ord(c) - 96 :]:
if left[t] <= 0:
continue
left[t] -= 1
ans[i] = t
del ans[i + 1 :]
for k in ascii_lowercase:
ans.extend([k] * left[k])
return "".join(ans + odd + ans[::-1])
return ""