[Twitter] Given a matrix with all elements sorted on each individual row and column find
来源:互联网 发布:丹江口水电站待遇 知乎 编辑:程序博客网 时间:2024/05/29 15:15
每次找到每行要比较的最小的列。
逐行比较
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int k_thsmall(vector<vector<int> > a, int k)
{
int n=a.size();
int m=a[0].size();
vector<int> minCol(n, 0);
minCol[0]=1;
int tmin=a[0][0];
int r;
for(int j=1; j<k; ++j)
{
tmin=INT_MAX;
for(int i=0; i<n; ++i)
{
if(minCol[i]<m)
{
if(a[i][minCol[i]]<tmin)
{
tmin=a[i][minCol[i]];
r=i;
}
}
}
minCol[r]++;
}
return tmin;
}
int main()
{
int a1[]={1,3,4,6,8};
int a2[]={2,9,10,12,17};
int a3[]={5,11,14,15,19};
int a4[]={7,13,16,18,20};
vector<vector<int> > vecs;
vector<int> vec1(a1,a1+5);
vector<int> vec2(a2,a2+5);
vector<int> vec3(a3,a3+5);
vector<int> vec4(a4,a4+5);
vecs.push_back(vec1);
vecs.push_back(vec2);
vecs.push_back(vec3);
vecs.push_back(vec4);
cout<<k_thsmall(vecs, 20)<<endl;
}
0 0
- [Twitter] Given a matrix with all elements sorted on each individual row and column find
- Maximal Square,Given a 2D binary matrix filled with 0's and 1's, find the largest squar
- Ruby: How to find all indices of elements that match a given condition?
- [LinkedIn]Implement Find and replace (find given pattern and replace it with a given string)
- [每日练习] write a function to find the key in a m*n matrix, where each line and column is incremental
- boost sparse matrix row and column
- Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or
- Find k closest elements to a given value
- Given a list of presentations with begin and end time that all need to use a conference room.
- Running a function on multiple elements using Jquery .each()
- Find Elements in Matrix
- mysql query to find the maximum 3 values from a column and group by on other column
- Given an infinite size array with only 0s and 1s and sorted. find the transition point where 0s end
- A dynamic niching clustering algorithm based on individual-connectedness and its application to colo
- Find Elements in Matrix-LintCode
- 有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
- 【Leetcode】Given a binary tree, find all root-to-leaf paths whose sum equals a given number
- No row with the given identifier exists
- Cocos2d-x v3.1 GUI系统--环境构建(七)
- 并发过10万的TCP请求 ,对Linux内核优化配置
- Write a function that computes log2() using sqrt()
- 希尔排序(ShellSort)
- servlet调用的几种方式
- [Twitter] Given a matrix with all elements sorted on each individual row and column find
- 数据挖掘领域顶级会议期刊及其分析
- ajax,jquery,$.post/$.get异步刷新div,局部刷新页面
- C++基础(六)一个Class
- 使用了<input type="submit" value="Submit" onclick="this.disabled=true" />不能提交
- 开启 J2EE(六)— Servlet之Filter详解及乱码处理实例
- windowAPI多线程
- Web Service 那点事儿-基本概念和JDK发布调用
- TexturePacker 3.0 使用教程