1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

本文介绍了一种结合动态规划与二分查找的算法,用于在一个给定的矩阵中找到最大边长的正方形,其元素之和不超过特定阈值。通过实例演示了如何高效地解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.

 

Example 1:

Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output: 2
Explanation: The maximum side length of square with sum less than 4 is 2 as shown.

Example 2:

Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
Output: 0

Example 3:

Input: mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6
Output: 3

Example 4:

Input: mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184
Output: 2

 

Constraints:

  • 1 <= m, n <= 300
  • m == mat.length
  • n == mat[i].length
  • 0 <= mat[i][j] <= 10000
  • 0 <= threshold <= 10^5

思路:DP+二分

class Solution(object):
    def maxSideLength(self, mat, threshold):
        """
        :type mat: List[List[int]]
        :type threshold: int
        :rtype: int
        """
        import math
        m,n = len(mat),len(mat[0])
        dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
        for i in range(1,m+1):
            for j in range(1,n+1):
                dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+mat[i-1][j-1]

        def ok(res):
            for i in range(res,m+1):
                for j in range(res,n+1):
                    t = dp[i][j]-dp[i][j-res]-dp[i-res][j]+dp[i-res][j-res]
                    if t<=threshold: return True
            return False

        lo,hi = 0,min(m,n)
        res = 0
        while lo<=hi:
            mid=(lo+hi)//2
            if ok(mid):
                res = max(res,mid)
                lo=mid+1
            else:
                hi=mid-1
        return res



s=Solution()
print(s.maxSideLength(mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4))
print(s.maxSideLength(mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1))
print(s.maxSideLength(mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6))
print(s.maxSideLength(mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184))

 

这个错误通常是由于指定的载频频率超过了采样率的一半导致的。在使用 `fskmod` 函数时,必须确保指定的载频频率不超过采样率的一半,以避免出现混叠效应。 混叠是指当信号的频率高于采样率的一半时,信号的高频部分会混叠到低频部分,从而导致信号失真。因此,为了避免混叠效应,必须确保信号的最高频率不超过采样率的一半。 要解决这个问题,您可以选择降低载频频率或增加采样率。如果您需要使用较高的载频频率,则需要增加采样率以避免混叠效应。如果您无法增加采样率,则需要降低载频频率以确保不超过采样率的一半。 以下是一个示例代码,展示了如何避免这个错误: ```matlab % 生成随机比特序列 N = 1000; % 比特序列长度 M = 2; % FSK 调制阶数 data = randi([0 M-1], N, 1); % 随机生成 0 到 M-1 的整数比特序列 % FSK 调制 fc1 = 3000; % 第一个载频频率 fc2 = 4000; % 第二个载频频率 fs = 8000; % 采样率 T = 1/fs; % 采样时间间隔 t = 0:T:(N/fs)-T; % 时间向量 % 检查载频频率是否超过采样率的一半 if fc1 > fs/2 || fc2 > fs/2 error('The maximum frequency must be less than or equal to Fs/2.'); end s = fskmod(data, M, fc1, fc2, fs); % FSK 调制 % 显示调制结果 subplot(2,1,1); plot(t, s); title('FSK 调制信号'); % FSK 解调 f1 = fskdemod(s, M, fc1, fs); % 解调得到第一个载频信号 f2 = fskdemod(s, M, fc2, fs); % 解调得到第二个载频信号 fsk_filt = medfilt1(f1-f2, 5); % 差分后进行中值滤波 % 显示解调结果 subplot(2,1,2); plot(t, fsk_filt); title('FSK 解调信号'); ``` 在这个示例代码中,我将第一个载频频率设置为 3000Hz,第二个载频频率设置为 4000Hz,但在调制之前,我使用了一个 `if` 语句来检查这两个频率是否超过了采样率的一半。如果超过了,则会出现错误提示。如果未超过,则进行 FSK 调制和解调,并将信号进行中值滤波。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值