classSolution: defareaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: x, y = 0, 0 for w, h in dimensions: if w**2 + h**2 > x**2 + y**2or w**2 + h**2 == x**2 + y**2and w * h > x * y: x, y = w, h return x * y
100187. 捕获黑皇后需要的最少移动次数
现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。
给你 6 个整数 a 、b 、c 、d 、e 和 f ,其中:
(a, b) 表示白色车的位置。
(c, d) 表示白色象的位置。
(e, f) 表示黑皇后的位置。
假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。
请注意:
车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。
象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。
如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。
皇后不能移动。
示例 1:
1 2 3 4
输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 输出:2 解释:将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。 由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。
示例 2:
1 2 3 4 5
输入:a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 输出:1 解释:可以通过以下任一方式移动 1 次捕获黑皇后: - 将白色车移动到 (5, 2) 。 - 将白色象移动到 (5, 2) 。
classSolution: def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int: if a == e: if c != a or (c == a and (d <= min(b, f) or d >= max(b, f))): return1 if b == f: if d != b or (d == b and (c <= min(a, e) or c >= max(a, e))): return1 ifabs(c - e) == abs(d - f): if (c - e) * (b - f) == (a - e) * (d - f): if a < min(c, e) or a > max(c, e): return1 else: return1 return2
100150. 移除后集合的最多元素数
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们的长度都是偶数n 。
你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1 和 nums2 中剩下的元素插入到集合 s 中。
classSolution: defmaximumSetSize(self, nums1: List[int], nums2: List[int]) -> int: cnt1, cnt2 = set(nums1), set(nums2) c1 = sum(1for x in cnt1 if x notin cnt2) c2 = sum(1for x in cnt2 if x notin cnt1) c = len(cnt1) - c1 returnmin(len(nums1), min(c1, len(nums1) // 2) + min(c2, len(nums1) // 2) + c)
def maxPartitionsAfterOperations(self, s: str, k: int) -> int: if k == 26: return1 A = list(ord(c) - ord('a') for c in s) n = len(A)
@cache def dp(i, s, t, cur): if i == n: return cur s2 = s | (1 << A[i]) res = 0 if s2.bit_count() > k: res = max(res, dp(i + 1, 1 << A[i], t, cur + 1)) else: res = max(res, dp(i + 1, s2, t, cur)) if t > 0: for j in range(26): s2 = s | (1 << j) if s2.bit_count() > k: res = max(res, dp(i + 1, 1 << j, t - 1, cur + 1)) else: res = max(res, dp(i + 1, s2, t - 1, cur)) return res returndp(0, 0, 1, 1)