[Leetcode Weekly Contest]174

链接:LeetCode

[Leetcode]5328. 方阵中战斗力最弱的 K 行

给你一个大小为?m?* n?的方阵?mat,方阵由若干军人和平民组成,分别用 0 和 1 表示。
请你返回方阵中战斗力最弱的?k?行的索引,按从最弱到最强排序。
如果第?i?行的军人数量少于第?j?行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。
军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。

输入:\(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\)
输出:\([2,0,3]\)

按照题意排序即可。

class Solution:
    def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
        res = []
        n,m = len(mat),len(mat[0])
        for i in range(n):
            num = len([x for x in mat[i] if x==1])
            res.append([i,num])
        res.sort(key=lambda x:[x[1],x[0]])
        return [x[0] for x in res][:k]

[Leetcode]5329. 数组大小减半

给你一个整数数组 arr。你可以从中选出一个整数集合,并删除这些整数在数组中的每次出现。
返回至少能删除数组中的一半整数的整数集合的最小大小。
统计每个数出现的次数,然后排序即可。

import collections
class Solution:
    def minSetSize(self, arr: List[int]) -> int:
        count = collections.Counter(arr)
        num = sorted(count.values())
        res = 0
        all_del = 0
        for n in num[::-1]:
            all_del+=n
            res += 1
            if all_del>=len(arr)//2:
                return res

[Leetcode]5330. 分裂二叉树的最大乘积

给你一棵二叉树,它的根为?root 。请你删除 1 条边,使二叉树分裂成两棵子树,且它们子树和的乘积尽可能大。由于答案可能会很大,请你将结果对 10^9 + 7 取模后再返回。
要计算子树和的乘积,不如只计算一个子树的和,然后将二叉树所有和减去该树即可得另一个字数和。如此,我们只需要计算每一个字数的和,从中选取一个能组成最大子树和乘积即可。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxProduct(self, root: TreeNode) -> int:
        res = 0
        dic = {}
        all_sum = self.dfs(root,dic)
        for val in dic.values():
            res = max(res,val*(all_sum-val))
        return res%(10**9 + 7)

    def dfs(self,root,dic):
        if not root:
            return 0
        if not root.left and not root.right:
            dic[root] = root.val
        else:
            dic[root] = root.val + self.dfs(root.left,dic) + self.dfs(root.right,dic)
        return dic[root]

[Leetcode]5331. 跳跃游戏 V

给你一个整数数组?arr 和一个整数?d 。每一步你可以从下标?i?跳到:

  • i + x?,其中?i + x < arr.length?且?0 < x <= d?。
  • i - x?,其中?i - x >= 0?且?0 < x <= d?。

除此以外,你从下标?i 跳到下标 j?需要满足:arr[i] > arr[j]?且 arr[i] > arr[k]?,其中下标?k?是所有 i?到 j?之间的数字(更正式的,min(i, j) < k < max(i, j))。
你可以选择数组的任意下标开始跳跃。请你返回你最多可以访问多少个下标。

利用动态规划的思想,令dp[i]为从位置i开始所能访问的最大下标数,则动态规划的递推式为:
\(dp[i] = max(dp[i],dp[j]+1)\),其中j为位置i可达点的下标。
其实,想出动态规划的递推式不难,关键在于我们怎么保证在计算dp[i]时,dp[j]可知。明显的,在数组中,我们在最低的点没有办法进行跳跃,则从该索引i出发最多访问1个下标;那么,我们再从第二低的点出现,此时,该点要么无法跳跃,要么只能跳最低的点,而此时\(dp[i]=1\)。由此可知,我们将整个数组从最小值开始遍历,到最大值,即可保证为全局最佳解。而这,与我们平时的从左到右的遍历不同,也是该问题的关键点了。

class Solution:
    def maxJumps(self, arr, d) :
        index_num = list(enumerate(arr))
        index_num.sort(key = lambda x:x[1])
        n = len(arr)
        dp = [1 for i in range(n)]
        res = 1
        for index,num in index_num:
            for i in reversed(range(max(0,index-d),index)):
                if arr[i] >= num:
                    break
                dp[index] = max(dp[index],dp[i]+1)
            for i in range(index+1,min(index+d+1,n)):
                if arr[i] >= num:
                    break
                dp[index] = max(dp[index],dp[i]+1)
            res = max(res,dp[index])
        return res

原文地址:https://www.cnblogs.com/hellojamest/p/12253657.html

时间: 2024-08-01 22:32:52

[Leetcode Weekly Contest]174的相关文章

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

Leetcode Weekly Contest 152

退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha

108th LeetCode Weekly Contest Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

113th LeetCode Weekly Contest Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip opera

113th LeetCode Weekly Contest Largest Time for Given Digits

Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour time is 00:00, and the largest is 23:59.  Starting from 00:00, a time is larger if more time has elapsed since midnight. Return the answer as a string

113th LeetCode Weekly Contest Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. Now, you do the following steps repeatedly, until all cards are revealed: Take the

116th LeetCode Weekly Contest Maximum Width Ramp

Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The width of such a ramp is j - i. Find the maximum width of a ramp in A.  If one doesn't exist, return 0. Example 1: Input: [6,0,8,2,1,5] Output: 4 Explanatio

LeetCode Weekly Contest 118

要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerfu