Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest. A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, that is, always ones may appear first and then zeros.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Input: mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 Output: [2,0,3] Explanation: The number of soldiers for each row is: row 0 -> 2 row 1 -> 4 row 2 -> 1 row 3 -> 2 row 4 -> 5 Rows ordered from the weakest to the strongest are [2,0,3,1,4]
题面很简单,其实这道题就是二进制的处理,Python 里面就暴力出奇迹了
1 2 3 4 5 6 7 8 9 10 11 12
from typing importList
classSolution: defkWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: ifnot mat: return [] number = [] for i inrange(len(mat)): number.append((int("".join([str(x) for x in mat[i]]), 2), i)) number.sort() return [x for _, x in number[0:k]]
1342. Reduce Array Size to The Half
描述:
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array. Return the minimum size of the set so that at least half of the integers of the array are removed.
1 2 3 4 5
Input: arr = [3,3,3,3,5,5,5,2,2,7] Output: 2 Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
classSolution: defminSetSize(self, arr: List[int]) -> int: ifnot arr: return0 counter = {} for i in arr: counter[i] = counter.setdefault(i, 0) + 1 counter = {k: v for k, v insorted(counter.items(), key=lambda item: item[1], reverse=True)} total_count = 0 result_count = 0 for i, count in counter.items(): total_count += count result_count += 1 if total_count >= len(arr) / 2: break return result_count
1343. Maximum Product of Splitted Binary Tree
描述:
Given a binary tree root. Split the binary tree into two subtrees by removing 1 edge such that the product of the sums of the subtrees are maximized. Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
1 2 3
Input: root = [1,2,3,4,5,6] Output: 110 Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.
1 2 3 4 5
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1.
这题的题面是这样,一个数组,里面有若干值,你可以从任意一个位置开始跳跃,一次只能跳一个,跳的时候需要满足规则,假定你从数组 i 位置起跳,每次可跳的范围是 x,那么你需要满足
i+x < arr.length 和 0<x<=d
i-x >=0 和 0<x<=d
同时假设你从 i 跳往 j,那么你需要保证 arr[i]>arr[j] 且 i 到 j 中的每个元素都满足 arr[j]<x<arr[i],求最多能跳多少个元素
classSolution: defmaxJumps(self, arr: List[int], d: int) -> int: length = len(arr) dp = [1] * length for a, i insorted([a, i] for i, a inenumerate(arr)): for di in [-1, 1]: for j inrange(i + di, i + d * di + di, di): ifnot (0 <= j < length and arr[j] < arr[i]): break dp[i] = max(dp[i], dp[j] + 1)