363. Max Sum of Rectangle No Larger Than K

本文介绍了一种高效求解最大子矩阵和问题的算法,并通过动态规划思想将其转化为最大子数组和问题进行解决,适用于矩阵中寻找不超过特定值k的最大子矩阵和。

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

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.

Example:

Given matrix = [
  [1,  0, 1],
  [0, -2, 3]
]
k = 2

The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).

Note:

  1. The rectangle inside the matrix must have an area > 0.
  2. What if the number of rows is much larger than the number of columns?

Credits:

Special thanks to @fujiaozhu for adding this problem and creating all test cases.



摘自

https://discuss.leetcode.com/topic/48875/accepted-c-codes-with-explanation-and-references/4

http://www.cnblogs.com/fll/archive/2008/05/17/1201543.html


先看一下最大子矩阵问题。


给定一个n*n(0<n<=100)的矩阵,请找到此矩阵的一个子矩阵,并且此子矩阵的各个元素的和最大,输出这个最大的值。
Example:
 0 -2 -7  0 
 9  2 -6  2 
-4  1 -4  1 
-1  8  0 -2 
其中左上角的子矩阵:
 9 2 
-4 1 
-1 8 
此子矩阵的值为9+2+(-4)+1+(-1)+8=15。
  我们首先想到的方法就是穷举一个矩阵的所有子矩阵,然而一个n*n的矩阵的子矩阵的个数当n比较大时时一个很大的数字 O(n^2*n^2),显然此方法不可行。
  怎么使得问题的复杂度降低呢?对了,相信大家应该知道了,用动态规划。对于此题,怎么使用动态规划呢?

  让我们先来看另外的一个问题(最大子段和问题):
    给定一个长度为n的一维数组a,请找出此数组的一个子数组,使得此子数组的和sum=a[i]+a[i+1]+……+a[j]最大,其中i>=0,i<n,j>=i,j<n,例如
   31 -41 59 26 -53  58 97 -93 -23 84
 子矩阵59+26-53+58+97=187为所求的最大子数组。
第一种方法-直接穷举法:
   maxsofar=0;
   for i = 0 to n
   {
       for  j = i to n 
       {
            sum=0;
            for k=i to j 
                sum+=a[k] 
            if (maxsofar>sum)
               maxsofar=sum;
       }
   }

第二种方法-带记忆的递推法:
   cumarr[0]=a[0]
   for i=1 to n      //首先生成一些部分和
   {
        cumarr[i]=cumarr[i-1]+a[i];       
   }

   maxsofar=0
   for i=0 to n
   {
       for  j=i to n     //下面通过已有的和递推
       {
           sum=cumarr[j]-cumarr[i-1]
           if(sum>maxsofar)
               maxsofar=sum
       }
   }
显然第二种方法比第一种方法有所改进,时间复杂度为O(n*n)。

下面我们来分析一下最大子段和的子结构,令b[j]表示从a[0]~a[j]的最大子段和,b[j]的当前值只有两种情况,(1) 最大子段一直连续到a[j]  (2) 以a[j]为起点的子段,不知有没有读者注意到还有一种情况,那就是最大字段没有包含a[j],如果没有包含a[j]的话,那么在算b[j]之前的时候我们已经算出来了,注意我们只是算到位置为j的地方,所以最大子断在a[j]后面的情况我们可以暂时不考虑。
由此我们得出b[j]的状态转移方程为:b[j]=max{b[j-1]+a[j],a[j]},
所求的最大子断和为max{b[j],0<=j<n}。进一步我们可以将b[]数组用一个变量代替。
得出的算法如下:
    int maxSubArray(int n,int a[])
    {
        int b=0,sum=-10000000;
        for(int i=0;i<n;i++)
        {
             if(b>0) b+=a[i];
             else b=a[i];
             if(b>sum) sum=b;  
        }
        return sum;
    }
这就是第三种方法-动态规划。


  现在回到我们的最初的最大子矩阵的问题,这个问题与上面所提到的最大子断有什么联系呢?
  假设最大子矩阵的结果为从第r行到k行、从第i列到j列的子矩阵,如下所示(ari表示a[r][i],假设数组下标从1开始):
  | a11 …… a1i ……a1j ……a1n |
  | a21 …… a2i ……a2j ……a2n |
  |  .     .     .    .    .     .    .   |
  |  .     .     .    .    .     .    .   |
  | ar1 …… ari ……arj ……arn |
  |  .     .     .    .    .     .    .   |
  |  .     .     .    .    .     .    .   |
  | ak1 …… aki ……akj ……akn |
  |  .     .     .    .    .     .    .   |
  | an1 …… ani ……anj ……ann |

 那么我们将从第r行到第k行的每一行中相同列的加起来,可以得到一个一维数组如下:
 (ar1+……+ak1, ar2+……+ak2, ……,arn+……+akn)
 由此我们可以看出最后所求的就是此一维数组的最大子断和问题,到此我们已经将问题转化为上面的已经解决了的问题了。

此题的详细解答如下(Java描述):

import java.util.Scanner;
public class PKU_1050
{
     private int maxSubArray(int n,int a[])
      {
            int b=0,sum=-10000000;
            for(int i=0;i<n;i++)
            {
                  if(b>0) b+=a[i];
                  else b=a[i];
                  if(b>sum) sum=b;
            }
            return sum;  
      }
      private int maxSubMatrix(int n,int[][] array)
      {
            int i,j,k,max=0,sum=-100000000;
            int b[]=new int[101];
            for(i=0;i<n;i++)
            {
                  for(k=0;k<n;k++)//初始化b[]
                  {
                        b[k]=0;
                  }
                  for(j=i;j<n;j++)//把第i行到第j行相加,对每一次相加求出最大值
                  {
                        for(k=0;k<n;k++)
                        {
                              b[k]+=array[j][k];
                        }
                        max=maxSubArray(k,b);  
                        if(max>sum)
                        {
                                sum=max;
                        }
                  }
            }
            return sum;
      }
      public static void main(String args[])
      {
            PKU_1050 p=new PKU_1050();
            Scanner cin=new Scanner(System.in);
            int n=0;
            int[][] array=new int[101][101];
            while(cin.hasNext())
            {
                       n=cin.nextInt();   
                       for(int i=0;i<n;i++)
                       {
                                  for(int j=0;j<n;j++)
                                  {
                                             array[i][j]=cin.nextInt();
                                  }
                       }
                       System.out.println(p.maxSubMatrix(n,array));
            }
      }
}



这里有限制,和不能超过K,又要最接近,参考问题


find a subarray that contains the largest sum, constraint that sum is less than k


把一维当中求最大子序列和替换为这个问题的解,2维到1维的处理过程不变。

public int maxSumSubmatrix(int[][] matrix, int k) {
    //2D Kadane's algorithm + 1D maxSum problem with sum limit k
    //2D subarray sum solution
    
    //boundary check
    if(matrix.length == 0) return 0;
    
    int m = matrix.length, n = matrix[0].length;
    int result = Integer.MIN_VALUE;
    
    //outer loop should use smaller axis
    //now we assume we have more rows than cols, therefore outer loop will be based on cols 
    for(int left = 0; left < n; left++){
        //array that accumulate sums for each row from left to right 
        int[] sums = new int[m];
        for(int right = left; right < n; right++){
            //update sums[] to include values in curr right col
            for(int i = 0; i < m; i++){
                sums[i] += matrix[i][right];
            }
            
            //we use TreeSet to help us find the rectangle with maxSum <= k with O(logN) time
            TreeSet<Integer> set = new TreeSet<Integer>();
            //add 0 to cover the single row case
            set.add(0);
            int currSum = 0;
            
            for(int sum : sums){
                currSum += sum;
                //we use sum subtraction (curSum - sum) to get the subarray with sum <= k
                //therefore we need to look for the smallest sum >= currSum - k
                Integer num = set.ceiling(currSum - k);
                if(num != null) result = Math.max( result, currSum - num );
                set.add(currSum);
            }
        }
    }
    
    return result;
}


# Find Rects Example # # This example shows off how to find rectangles in the image using the quad threshold # detection code from our April Tags code. The quad threshold detection algorithm # detects rectangles in an extremely robust way and is much better than Hough # Transform based methods. For example, it can still detect rectangles even when lens # distortion causes those rectangles to look bent. Rounded rectangles are no problem! # (But, given this the code will also detect small radius circles too)... import time, os, gc, sys from media.sensor import * from media.display import * from media.media import * from machine import UART import time from machine import PWM, FPIOA #servo def find_majority_element(nums): """ 查找数组中出现次数超过一半的元素(众数) 参数: nums (list): 整数数组 返回: int or None: 众数(如果存在),否则返回None """ candidate = None # 候选众数 count = 0 # 计数器 # 第一轮遍历:找出候选众数 for num in nums: if count == 0: candidate = num # 当计数器为0时,更新候选众数 count += (1 if num == candidate else -1) # 相同则加1,不同则减1 # 验证候选众数是否确实超过半数 count = 0 for num in nums: if num == candidate: count += 1 # 如果出现次数超过数组长度的一半,则返回候选众数 return candidate if count > len(nums) // 2 else None #pid class PID: def __init__(self, kp, ki, input_value, target=320): self.e = 0 self.e_last = 0 self.kp = kp self.ki = ki self.target = target self.input_value = input_value def cal(self, value): self.e = self.target - value delta = self.kp * (self.e-self.e_last) + self.ki * self.e self.e_last = self.e self.input_value = self.input_value + delta return self.input_value c_x=160 c_y=120 pid_x = PID(-0.002, -0.0003, 1.5/20*100, c_x) # 垂直控制PID: 比例系数-0.002, 积分系数-0.0003, 初始控制值1.5ms pid_y = PID(-0.002, -0.0003, 0.5/20*100, c_y) #image DETECT_WIDTH = ALIGN_UP(320, 16) DETECT_HEIGHT = 240 sensor = None color_threshold =(71, 100, -15, 15, -34, -3) #color_threshold =((97, 100, -14, -4, -45, -2)) def camera_init(): global sensor # construct a Sensor object with default configure sensor = Sensor(width=DETECT_WIDTH,height=DETECT_HEIGHT) # sensor reset sensor.reset() # set hmirror # sensor.set_hmirror(False) # sensor vflip # sensor.set_vflip(False) # set chn0 output size sensor.set_framesize(width=DETECT_WIDTH,height=DETECT_HEIGHT) # set chn0 output format sensor.set_pixformat(Sensor.RGB565) # use IDE as display output Display.init(Display.VIRT, width= DETECT_WIDTH, height = DETECT_HEIGHT,fps=100,to_ide = True) # init media manager MediaManager.init() # sensor start run sensor.run() #######################image_function def camera_deinit(): global sensor # sensor stop run sensor.stop() # deinit display Display.deinit() # sleep os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100) # release media buffer MediaManager.deinit() def find_min_b(blobs): min_size=1000000 for blob in blobs: if min_size > blob[2]*blob[3] : min_blob=blob min_size = blob[2]*blob[3] return min_blob def find_max_c(circles): max_size=0 for circle in circles: if circle[3] > max_size: max_circle=circle max_size = circle[3] return max_circle def find_max_r(rects): max_size=0 for rect in rects: if rect[2]*rect[3] > max_size: max_rect=rect max_size = rect[2]*rect[3] return max_rect #############################servo_function def pwm_init(): pwm_io1 = FPIOA() pwm_io1.set_function(47, FPIOA.PWM3) pwm_shang = PWM(3, 50, 50, enable=True) # 配置PWM3,默认频率50Hz,占空比50%,xia # 配置排针引脚号32,芯片引脚号为46的排针复用为PWM通道2输出 pwm_io2 = FPIOA() pwm_io2.set_function(46, FPIOA.PWM2) pwm_xia = PWM(2, 50, 50, enable=True) # 配置PWM2,默认频率50Hz,占空比50%,shang pwm_xia.duty(7.5) #xia舵机旋转到中间 pwm_shang.duty(2.5) #shang舵机旋转到中间 def capture_picture(): os.exitpoint() global sensor img = sensor.snapshot() # `threshold` below should be set to a high enough value to filter out noise # rectangles detected in the image which have low edge magnitudes. Rectangles # have larger edge magnitudes the larger and more contrasty they are... for c in img.find_circles(threshold = 10000): #img.draw_circle([v for v in r.circle()], color = (255, 0, 0)) img.draw_circle(c.x(), c.y(), c.r(), color=(255,0,0)) #for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) img.draw_cross(c.x(), c.y(),color = (255, 0, 0)) print(c) #for r in img.find_rects(threshold = 10000): # img.draw_rectangle([v for v in r.rect()], color = (255, 255, 255)) # #for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) # print(r) #img_rect = img.to_grayscale(copy=True) #img_rect = img_rect.binary([(23, 62)]) #rects=img_rect.find_rects(threshold =2000) blobs=None global flag #global max_blob global max_rect_ #max_blob=None #max_rect_=None #if rects: # max_rect_= find_max_r(rects) # img.draw_rectangle([v for v in max_rect_.rect()], color = (255, 255, 255)) # blobs = img.find_blobs([color_threshold],roi=max_rect_.rect(),merge=True,pixels_threshold=2,area_threshold=2) for i in range(6): img_rect = img.to_grayscale(copy=True) img_rect = img_rect.binary([(23, 62)]) rects = img_rect.find_rects(threshold=10000) # 最小面积阈值10000像 # 绘制找到的矩形 if not rects == None: for rect in rects: corner = rect.corners() # 获取矩形四个角点 # 绘制矩形边界 img.draw_line(corner[0][0], corner[0][1], corner[1][0], corner[1][1], color=(0, 255, 0), thickness=1) img.draw_line(corner[2][0], corner[2][1], corner[1][0], corner[1][1], color=(0, 255, 0), thickness=1) img.draw_line(corner[2][0], corner[2][1], corner[3][0], corner[3][1], color=(0, 255, 0), thickness=1) img.draw_line(corner[0][0], corner[0][1], corner[3][0], corner[3][1], color=(0, 255, 0), thickness=1) # 计算矩形中心点 c_x = sum([corner[k][0] for k in range(4)])/4 c_y = sum([corner[k][1] for k in range(4)])/4 if len(rects) == 2: print("center_point: {}".format([round(c_x), round(c_y)])) flag = 1 # 进入追踪模式 pid_x.target = c_x pid_y.target = c_y max_rect_= find_max_r(rects) t=0 t=t+1 blobs = img.find_blobs([color_threshold],roi=max_rect_.rect(),merge=True,pixels_threshold=2,area_threshold=2) #else flag=0 if blobs: max_blob = find_min_b(blobs)#调用函数,返回最大色块 #img.draw_circle(80,60,5,color=200) img.draw_circle(max_blob.cx(),max_blob.cy(),10,color=200) img.draw_rectangle((max_blob.x(),max_blob.y(),max_blob.w(),max_blob.h()),color=(255,0,0))#用红色框出最大色块 img.draw_string_advanced(0,0, "(x,y) =") img.draw_string_advanced(max_blob.x()+40,max_blob.y(), str(max_blob.cx())) img.draw_string_advanced(max_blob.x()+60,max_blob.y(), str(max_blob.cy()))#在框图显示色块的中心坐标 img.draw_string_advanced(40,0, str(max_blob.cx())) img.draw_string_advanced(60,0, str(max_blob.cy()))#在框图左上角显示色块的中心坐标 c_x = max_blob.x() + max_blob.w() / 2 c_y = max_blob.y() + max_blob.h() / 2 new_duty = pid_x.cal(c_x) # 计算新占空比 # 限制占空比在0.5ms-2.5ms范围内(舵机安全范围) new_duty = max(min(new_duty, 2.5/20*100), 0.5/20*100) # 更新水平舵机位置 pwm_xia.enable(0) # 临时禁用PWM pwm_xia.duty(round(new_duty, 2)) # 设置新占空比 pwm_xia.enable(1) # 重新启用PWM # 垂直方向PID控制 new_duty = pid_y.cal(c_y) # 计算新占空比 # 限制占空比在安全范围内 new_duty = max(min(new_duty, 2.5/20*100), 0.5/20*100) # 更新垂直舵机位置 pwm_shang.enable(0) # 临时禁用PWM pwm_shang.duty(round(new_duty, 2)) # 设置新占空比 print(round(new_duty, 1)) # 调试输出 pwm_shang.enable(1) # 重新启用PWM # draw result to screen Display.show_image(img) img = None gc.collect() def main(): os.exitpoint(os.EXITPOINT_ENABLE) camera_is_init = False fps = time.clock() pwm_init() try: print("camera init") camera_init() camera_is_init = True print("camera capture") while True: fps.tick() try: capture_picture() except KeyboardInterrupt as e: print("user stop: ", e) break except BaseException as e: print(f"Exception {e}") break print(fps.fps()) except Exception as e: print(f"Exception {e}") finally: if camera_is_init: print("camera deinit") camera_deinit() if __name__ == "__main__": main() 这段代码报错Exception Not enough positional arguments!
最新发布
08-01
根据提供的引用内容,当使用kivy.graphics.vertex_instructions.Rectangle对象时,如果尝试访问其rgba属性,可能会出现AttributeError: 'kivy.graphics.vertex_instructions.Rectangle' object has no attribute 'rgba'的错误。 这个错误是由于kivy.graphics.vertex_instructions.Rectangle对象没有名为rgba的属性导致的。可能是因为该对象没有定义或者使用了错误的属性名称。 为了解决这个错误,您可以检查代码中对kivy.graphics.vertex_instructions.Rectangle对象的使用,并确保正确地访问其属性。您可以查看相关文档或示例代码以了解正确的属性名称和用法。 以下是一个示例代码,演示了如何使用kivy.graphics.vertex_instructions.Rectangle对象并访问其属性: ```python from kivy.app import App from kivy.uix.widget import Widget from kivy.graphics import Rectangle class MyWidget(Widget): def __init__(self, **kwargs): super(MyWidget, self).__init__(**kwargs) # 创建一个Rectangle对象 rect = Rectangle(pos=(100, 100), size=(200, 200)) # 访问Rectangle对象的属性 print(rect.pos) # 输出:(100, 100) print(rect.size) # 输出:(200, 200) #print(rect.rgba) # 这里会报错,因为Rectangle对象没有rgba属性 class MyApp(App): def build(self): return MyWidget() if __name__ == '__main__': MyApp().run() ``` 请注意,在上面的示例代码中,我们尝试访问了Rectangle对象的pos和size属性,这是正确的。但是,我们注释掉了访问rgba属性的代码,因为Rectangle对象没有定义该属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值