current position:Home>Finding the k-th small element of ordered matrix (dichotomy idea)
Finding the k-th small element of ordered matrix (dichotomy idea)
2022-04-29 09:26:44【Sword laii】
Their thinking
Two points search , Convert two dimensions to one dimension ,check The function is used to solve less than or equal to target The number of elements is the same as k The relationship between
Code
class Solution {
public:
// Statistical satisfaction less than target Number of array elements , exceed k return true, Not more than k return false
bool check(vector<vector<int>>& matrix, int cnt,int target,int k){
int num = 0;
int i = cnt-1;
int j = 0;
while(i>=0 && j<cnt){
// Start at the lower left corner of the matrix , And statistics of the second j Column to the first i Number of elements in the row
if(matrix[i][j]<=target){
// Find each column <=target Element location of
j++;
num+=i+1;
}else{
i--;
}
}
return num>=k;
}
// Two points search
int kthSmallest(vector<vector<int>>& matrix, int k) {
int cnt = matrix.size();
int left = matrix[0][0];
int right = matrix[cnt-1][cnt-1];
while(left<right){
// Find the leftmost boundary
int mid = left + (right-left)/2;
if(check(matrix,cnt,mid,k)){
right = mid;
}else{
left = mid+1;
}
}
return left;
}
};
copyright notice
author[Sword laii],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290648003253.html
The sidebar is recommended
- Lesson 3 of ROS quick start - subscriber subscriber of ROS
- A lifeless face
- Mock in Vue JS preliminary simple use
- The Java Web servlet triggers the alert box on the front end
- CSS sets the color of the small vertical bar in front of the title
- Incomplete display of CSS background image
- [front end learning notes] save the front-end related codes
- Precautions for AWS serverless design dynamodb
- AWS serverless design - apigateway
- AWS serverless design lambda
guess what you like
AWS serverless design - firewall WAF
AWS serverless design-s3
Python repeated element determination function program
Nginx direction agent solves cross domain Problems-2
The foundation of JavaScript
DOM based on JavaScript
Javascript based BOM
JavaScript advanced functions
Basic summary of JavaScript advanced
Object oriented JavaScript
Random recommended
- JavaScript advanced threading mechanism and event mechanism
- HTML+CSS
- Introduction to less
- CSS3 media query
- Learn about bootstrap
- JQuery learning
- Ajax case
- Ajax sends a post request
- Ajax sends get requests
- Ajax notes
- Ajax learning notes
- Relearn react (1) - recognize the life cycle
- Small problems encountered by react usereducer and Solutions
- CSS realizes the square of adaptive screen width
- Nginx + ModSecurity setup
- Bootstrap web job
- bootstrap
- Swoft 2. X Foundation (HTTP, database, redis)
- Docker actual combat case 2: nginx load balancing
- Vue basic syntax
- Go basic syntax 3 (socket, httpserver)
- Nginx configures HTTPS and calls http
- Nginx parsing vulnerability
- How can the backend package Vue projects quickly
- Netty configures WSS protocol access through nginx (Practical)
- Total permutation problem_ Not containing the same element and containing the same element_ Leetcode46_ Leetcode47_ Backtracking solution + java code
- How to upload and download files using Axios in nodejs
- Vue excel file upload
- CondaHTTPError: HTTP 000 CONNECTION FAILED for url < https://conda.anaconda.org/fastai/noarch/repodat
- Multi function environmental monitoring pole scheme based on wireless gateway
- JPS in Hadoop, but http://hadoop01:50070/ How to solve the problem if there is no interface? 500ha70 cluster can't be accessed?
- Tool class for obtaining specific information in HTTP request
- UAV project tracking record 65 - wireless transceiver module circuit
- UAV project tracking record 76 - wireless transceiver module circuit
- Basic concept of angular velocity
- Front end login password encryption and decryption
- Vue parent-child component mutual transmission and intermodulation
- Vue nested loop array, select one of them and add the selected style
- The Vue seamless scroll list scrolls automatically
- Vue line graph moves dynamically to the right