leetcode 363. 矩形區域不超過 K 的最大數值和

給你一個 m x n 的矩陣 matrix 和一個整數 k ,找出并返回矩陣內部矩形區域的不超過 k 的最大數值和。

題目數據保證總會存在一個數值和不超過 k 的矩形區域。

示例 1:

輸入:matrix = [[1,0,1],[0,-2,3]], k = 2
輸出:2
解釋:藍色邊框圈出來的矩形區域 [[0, 1], [-2, 3]] 的數值和是 2,且 2 是不超過 k 的最大數字(k = 2)。

解題思路

枚舉上下邊界,確定了上下邊界以后,利用一維數組記錄每一列的和,就可以通過這個一維數組計算得出當前上下邊界固定的情況下所有子矩陣的和,公式為Sr-Sl<=k,Sr為前R列的矩陣和,Sl為前l列的矩陣和,Sr-Sl即為l-r列的子矩陣和,枚舉Sr,通過TreeSet查找滿足Sr-Sl<=k的Sl。

代碼

class Solution {public int maxSumSubmatrix(int[][] matrix, int k) {int n=matrix.length,m=matrix[0].length;int res=Integer.MIN_VALUE;for (int i = 0; i < n; i++) {int[] sum = new int[m];for (int j = i; j < n; j++) {for (int p = 0; p < m; p++) {sum[p]+=matrix[j][p];}TreeSet<Integer> set = new TreeSet<>();int cur=0;set.add(0);for (int l : sum) {cur+=l;//sr-k<=slInteger ceiling = set.ceiling(cur - k);if(ceiling!=null)res=Math.max(res,cur-ceiling);set.add(cur);}}}return res;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391365.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391365.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391365.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

centos7.4二進制安裝mysql

1&#xff1a;下載二進制安裝包&#xff08;安裝時確保沒有mysql數據庫服務器端&#xff09;&#xff1a; mariadb-10.2.12-linux-x86_64.tar.gz、 mariadb-10.2.12.tar.gz。2&#xff1a;創建系統賬號指定shell類型&#xff08;默認自動創建同名的組&#xff09;3&#xff1a;…

批梯度下降 隨機梯度下降_梯度下降及其變體快速指南

批梯度下降 隨機梯度下降In this article, I am going to discuss the Gradient Descent algorithm. The next article will be in continuation of this article where I will discuss optimizers in neural networks. For understanding those optimizers it’s important to…

java作業 2.6

//程序猿&#xff1a;孔宏旭 2017.X.XX /**功能&#xff1a;在鍵盤輸入一個三位數&#xff0c;求它們的各數位之和。 *1、使用Scanner關鍵字來實現從鍵盤輸入的方法。 *2、使用取余的方法將各個數位提取出來。 *3、最后將得到的各個數位相加。 */ import java.util.Scanner; p…

ubuntu 16.04 掛載新硬盤

2、掛載數據盤 mkdir /datausrubuntu:~$ lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 465.8G 0 disk sda1 8:1 0 512M 0 part /boot/efisda2 8:2 0 464.3G 0 part /sda3 8:3 0 976…

Go語言實戰 : API服務器 (2) 運行流程

1.API服務器的總流程 分為兩步&#xff1a; 啟動API服務器API服務器對HTTP請求進行處理 2.API服務器啟動流程 解析配置文件&#xff0c;利用配置文件完成對服務器的初始化配置初始化logger&#xff0c;開啟日志記錄與數據庫建立連接設置http連接&#xff08;例如設置響應頭…

Linux 命令 之查看程序占用內存

2019獨角獸企業重金招聘Python工程師標準>>> 查看PID ps aux | grep nginx root 3531 0.0 0.0 18404 832 ? Ss 15:29 0:00 nginx: master process ./nginx 查看占用資源情況 pmap -d 3531 top -p 3531 轉載于:https://my.oschina.net/mengzha…

邏輯回歸 自由度_回歸自由度的官方定義

邏輯回歸 自由度Back in middle and high school you likely learned to calculate the mean and standard deviation of a dataset. And your teacher probably told you that there are two kinds of standard deviation: population and sample. The formulas for the two a…

動畫電影的幕后英雄怎么說好_幕后編碼面試-好與壞

動畫電影的幕后英雄怎么說好Interviewing is a skill in and of itself. You can be the best developer in the world but you may still screw up an interview.面試本身就是一種技能。 您可以成為世界上最好的開發人員&#xff0c;但您仍可能會搞砸面試。 How many times h…

數據庫之DML

1、表的有關操作&#xff1a; 1.1、表的創建格式&#xff1a; CREATE TABLE IF NOT EXISTS 表名(屬性1 類型&#xff0c;屬性2 類型&#xff0c;....&#xff0c;屬性n 類型&#xff09;&#xff1b;# 標記部分表示可以省略 1.2、表的修改格式&#xff1a;ALTER table 表名 ADD…

網絡對抗技術作業一 201421410031

姓名&#xff1a;李冠華 學號&#xff1a;201421410031 指導教師&#xff1a;高見 1、虛擬機安裝與調試 安裝windows和linux&#xff08;kali&#xff09;兩個虛擬機&#xff0c;均采用NAT網絡模式&#xff0c;查看主機與兩個虛擬機器的IP地址&#xff0c;并確保其連通性。同時…

生存分析簡介:Kaplan-Meier估計器

In my previous article, I described the potential use-cases of survival analysis and introduced all the building blocks required to understand the techniques used for analyzing the time-to-event data.在我的上一篇文章中 &#xff0c;我描述了生存分析的潛在用例…

強密碼檢測

#!/usr/bin/env python # -*- coding: utf-8 -*- #1. 密碼長度應該大于或等于8位 #2. 密碼必須包含數字、字母及特殊字符三種組合 nums 0123456789 chars1 abcdefghijklmnopqrstuvwxyz chars2 ABCDEFGHIJKLMNOPQRSTUVWXYZ symbols r!#$%^&*()_-/*{}[]\|";:/?…

OD Linux發行版本

題目描述&#xff1a; Linux操作系統有多個發行版&#xff0c;distrowatch.com提供了各個發行版的資料。這些發行版互相存在關聯&#xff0c;例如Ubuntu基于Debian開發&#xff0c;而Mint又基于Ubuntu開發&#xff0c;那么我們認為Mint同Debian也存在關聯。 發行版集是一個或多…

Go語言實戰 : API服務器 (3) 服務器雛形

簡單API服務器功能 實現外部請求對API 服務器健康檢查和狀態查詢&#xff0c;返回響應結果 1.API服務器的狀態監測 以內存狀態檢測為例&#xff0c;獲取當前服務器的健康狀況、服務器硬盤、CPU 和內存使用量 func RAMCheck(c *gin.Context) {u, _ : mem.VirtualMemory()//獲…

TCP/IP協議-1

轉載資源&#xff0c;鏈接地址https://www.cnblogs.com/evablogs/p/6709707.html 轉載于:https://www.cnblogs.com/Chris-01/p/11474915.html

http://nancyfx.org + ASPNETCORE

商務產品servicestack&#xff1a; https://servicestack.net/ http://nancyfx.org ASPNETCORE http://nancyfx.org Drapper ORM精簡框架 https://github.com/StackExchange/Dapper Nancy 是一個輕量級用于構建基于 HTTP 的 Web 服務&#xff0c;基于 .NET 和 Mono 平…

使用r語言做garch模型_使用GARCH估計貨幣波動率

使用r語言做garch模型Asset prices have a high degree of stochastic trends inherent in the time series. In other words, price fluctuations are subject to a large degree of randomness, and therefore it is very difficult to forecast asset prices using traditio…

ARC下的內存泄漏

##ARC下的內存泄漏 ARC全稱叫 ARC(Automatic Reference Counting)。在編譯期間&#xff0c;編譯器會判斷對象的使用情況&#xff0c;并適當的加上retain和release&#xff0c;使得對象的內存被合理的管理。所以&#xff0c;從本質上說ARC和MRC在本質上是一樣的&#xff0c;都是…

python:校驗郵箱格式

# coding:utf-8import redef validateEmail(email):if re.match("^.\\(\\[?)[a-zA-Z0-9\\-\\.]\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) ! None:# if re.match("/^\w[a-z0-9]\.[a-z]{2,4}$/", email) ! None:print okreturn okelse:print failret…

cad2019字體_這些是2019年最有效的簡歷字體

cad2019字體When it comes to crafting the perfect resume to land your dream job, you probably think of just about everything but the font. But font is a key part of your first impression to recruiters and employers.當制作一份完美的簡歷來實現理想的工作時&…