当前位置:网站首页>Lesson 1: finding the minimum of a matrix
Lesson 1: finding the minimum of a matrix
2022-07-07 09:32:00 【Fight! Sao Nian!】
subject :AcWing 1452. Find the minimum of the matrix
Given a n×n Matrix , The matrix contains n×n individual Different from each other The integer of .
Define the minimum : If the value of a number is smaller than that of all adjacent numbers , Then this value is called the minimum .
The adjacent numbers of a number refer to the four adjacent numbers in the upper, lower, left and right directions , Also note , The number at the boundary or corner may have less than four adjacent numbers .
Ask for in O(nlogn) Find the position of any minimum within the time complexity , And output its rows and columns .
The matrix in this question is hidden , You can use our preset int function query To get the value of a certain position in the matrix .
for example ,query(a,b) You can get the... In the matrix a Xing di b The value of the position of the column .
Be careful :
The rows and columns of the matrix are from 0 Numbered starting .
query() Function cannot be called more than (n+2)×⌈log2n⌉+n.
The answer is not unique , Output the position of any minimum value .
Data range
1≤n≤300, The integers in the matrix are int Within the scope of .
sample input :
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sample output :
[0, 0]
Topic analysis :
The first way to think about it is to start from the starting point and find something smaller than yourself , Keep looking for , Then we will definitely find the minimum ( Because the points are different )
But the worst case is to visit n2 Time .
The second method is dichotomy , First find the minimum value in the middle column , Then look at the left and right
1. If v<l,v<r that v Is the minimum
2. If l<v, Then there is a minimum on the left
3. If r<v, Then there is a minimum on the right
Why? ?
Suppose the right r<v, So in the middle mid This column is better than r Be big , Then we use the first method to find that it is definitely impossible to cross mid This column ( Because I will look for something better than r Small number ), So in mid There must be a minimum on the right , This reduces the amount of search by half , Such repetition will finally find .
// Forward declaration of queryAPI.
// int query(int x, int y);
// return int means matrix[x][y].
class Solution {
public:
vector<int> getMinimumValue(int n) {
typedef long long LL;
const LL INF = 1e15;
int l=0,r=n-1;
while(l<r) // Dichotomy
{
int mid = l + r >> 1;
LL val=INF;
int k=0; // Which row to save is the smallest
for(int i=0;i<n;i++)
{
int t=query(i,mid);
if(t<val)
{
val=t;
k=i;
}
}
LL left=mid?query(k,mid-1):INF; // Query left
LL right=mid+1<n?query(k,mid+1):INF; // Query the right
if(val<left&&val<right)return {
k,mid}; // If the current point is the minimum
else if(left<val)r=mid-1;
else l=mid+1;
}
int k;
LL val=INF;
for(int i=0;i<n;i++)
{
int t=query(i,l);
if(t<val)
{
val=t;
k=i;
}
}
return {
k,l};
}
};
边栏推荐
- Interface test API case, data and interface separation
- STM32 and motor development (from stand-alone version to Networking)
- ComputeShader
- Huawei hcip datacom core_ 03day
- CMD startup software passes in parameters with spaces
- Network request process
- Self awakening from a 30-year-old female programmer
- Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
- Install pyqt5 and Matplotlib module
- NATAPP内网穿透
猜你喜欢

Postman interface test (II. Set global variables \ sets)

Jenkins task grouping

Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute

Netease cloud wechat applet

正则匹配以XXX开头的,XXX结束的

嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数

Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion

十二、排序

Information Security Experiment 3: the use of PGP email encryption software

Jemter operation
随机推荐
Jmeters use
How to solve the problem of golang select mechanism and timeout
網易雲微信小程序
数据建模中利用3σ剔除异常值进行数据清洗
Implementation of corner badge of Youmeng message push
Jenkins modifies the system time
Unity shader (to achieve a simple material effect with adjustable color attributes only)
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
印象笔记终于支持默认markdown预览模式
Unittest simple project
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
**grafana安装**
Regular matching starts with XXX and ends with XXX
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
DRF defines views and routes
DRF authentication, permissions, and flow restrictions (only for views in DRF)
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
Using JWT to realize login function
Difference between process and thread
ComputeShader