当前位置:网站首页>有序矩阵中的第 k 个最小数组和
有序矩阵中的第 k 个最小数组和
2022-07-20 15:18:00 【漫漫想想】
题目
给你一个 m * n 的矩阵 mat,以及一个整数 k ,矩阵中的每一行都以非递减的顺序排列。
你可以从每一行中选出 1 个元素形成一个数组。返回所有可能数组中的第 k 个 最小 数组和。
二分+dfs,很神奇的想法
#include<iostream>
#include<string>
#include<deque>
#include<cmath>
#include<vector>
#include<unordered_map>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int rowLength=0,colLength=0,kth=0,cnt=1;
void dfs(int row,int sum,int target,vector<vector<int>>& mat) {
//出口条件
if (sum > target || cnt > kth || row >= rowLength) return;
//第一列值之和
dfs(row+1,sum,target,mat);
//不同列之和
for(int i=1; i<colLength; i++) {
int curSum=sum-mat[row][0]+mat[row][i];
if (curSum > target) break;
cnt++;
dfs(row + 1, curSum, target,mat);
}
}
int kthSmallest(vector<vector<int>>& mat, int k) {
rowLength=mat.size(),colLength=mat[0].size(),kth=k;
//二分搜索头尾范围
int left = 0, right = 0;
for (int i = 0; i < rowLength; i++) {
left += mat[i][0];
right += mat[i][colLength - 1];
}
right++;
int init=left;
cout<<" left "<<left<<" right"<<right<<endl;
while(left<right) {
int mid=left+(right-left)/2;
cnt=1;
//行号 初始最小和 中间值 矩阵
dfs(0,init,mid,mat);
if(cnt<k) {
left=mid+1;
} else {
right=mid;
}
// cout<<"cnt "<<cnt<<" right"<<right<<endl;
}
return left;
}
int main()
{
int m, n,data,k;
vector<vector<int> > mat;
cin>>m>>n;
for(int i=0; i<m; i++)
{
vector<int> row;
for(int j=0; j<n; j++)
{
cin>>data;
row.push_back(data);
}
mat.push_back(row);
}
cin>>k;
int res=kthSmallest(mat,k);
cout<<res<<endl;
return 0;
}
边栏推荐
- 494.目标和·深度优先搜索·背包问题
- C语言力扣第八题之字符串转换整数。遍历法
- About Zend_ parse_ Parameters function
- Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
- [harmony OS] [FAQ] Hongmeng application development problem sharing (font / constructor)
- Write multiple main in the source file of vs stdio project
- 【BERT】模型返回值解析
- Azure安全基础知识
- 网易游戏 Flink SQL 平台化实践
- With the flow dividend receding, how can FMCG agents break through and grow with RPA?
猜你喜欢
【mindspore】【训练警告】执行训练代码时存在的警告
U-Net: Convolutional Networks for Biomedical Image Segmentation
494.目标和·深度优先搜索·背包问题
控制台报错 Uncaught TypeError: Cannot read properties of null (reading ‘append‘) 解决方案
[harmony OS] [FAQ] Hongmeng application development problem sharing (font / constructor)
===、==、Object.is 基本包装类型
语义分割-Rethinking BiSeNet For Real-time Semantic Segmentation-1-Cityscapes数据集
2022 new third-party pagoda panel btcloud PHP source code
[harmonyos] [arkui] Hongmeng linear gradient to achieve gradient, how to dynamically set it? I tried it for your reference
Good at C (day 70)
随机推荐
【HMS Core】【FAQ】【Health Kit】集成运动健康服务过程中,遇到一些小问题,今天分享给大家(华为手表、手环+运动健康服务问题合集)
AtomicInteger (计数器)的用法
DOS assembler improvement exercise
Nodejs 包
【HMS core】【Wallet Kit】【解决方案】华为钱包的客户端示例代码为何无法运行
[target detection] yolov1-v3 principle
R语言使用isna函数查看列表和dataframe中是否包含缺失值、将dataframe中数据列中的异常值标注为缺失值NA、使用na.omit函数删除dataframe中包含缺失值NA的数据行
Number of connected block midpoint (day 68)
【BERT】QA、阅读理解、信息检索
Skipped 60 frames! The application may be doing too much work on its main thread
CentOS 7 安装mysql
muduo网络库编程
【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考
LayoutInflater 布局渲染工具
DOS assembly debug basic command and its function explanation
[HMS core] [push kit] [FAQ] Huawei push service mobile phone does not receive push message / message delay / information screen notification problem collection
vscode安装及配置
[pytorch] torch geometric installation
U-Net: Convolutional Networks for Biomedical Image Segmentation
leetcode 1791. 找出星型图的中心节点