当前位置:网站首页>[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
2022-07-02 06:53:00 【tomly2020】
Day 35
1060 Missing elements in an ordered array
There is an existing one, press Ascending Array of arranged integers nums , Each of these numbers Different from each other .
Give you an integer k , Please find and return the first... From the leftmost side of the array k Missing number .
Method
Because the array is in ascending order , So we can consider using dichotomy to solve this problem , It's easy to know , For subscripts i The elements of , The number of empty elements between it and the first element is nums[i]-nums[0]-i, And what we are looking for is k Missing elements , therefore , We just need to find out The interval between and the first element should not exceed k The maximum subscript of , Then add and k The difference between the , It's the one we want to find k Missing elements .
class Solution {
public int missingElement(int[] nums, int k) {
int l = 0, r = nums.length - 1;
// Binary search for the largest subscript
while (l <= r){
int mid = (l + r) >> 1;
if (nums[mid] - nums[0] - mid >= k){
r = mid - 1;
}
else{
l = mid + 1;
}
}
return nums[r] + k - (nums[r] - nums[0] - r);
}
}
1901 Find the pinnacle element Ⅱ
One 2D In the grid Peak element That means Strictly greater than Its adjacent grid ( On 、 Next 、 Left 、 Right ) The elements of .
To give you one from 0 Numbered starting Of m x n matrix mat , The values of any two adjacent lattices are inequality . find Any one Peak element mat[i][j] and Return to its position [i,j] .
You can assume that the entire matrix is surrounded by a circle with a value of -1 Lattice of .
It is required that the time complexity must be written O(m log(n)) or O(n log(m)) The algorithm of
Method
According to the characteristics of the elements in the array , Because every number in the array is different , Then there must be such a peak element , And find the peak element , We can use the maximum value of column or row to deal with ( It is equivalent to a dimensionality reduction operation for the element group , We have the maximum value of each row or column to represent that row or column ). In this way, the problem is transformed into a one-dimensional array , Let's look for a peak .
For in a one-dimensional array , Algorithm for finding peak , We can use the idea of class dichotomy . The specific algorithm is as follows :
Definition l and r, Make mid=(l+r)/2, Let's compare mid and mid-1, That is to investigate the increase and decrease of this community , If we find that , In this neighborhood , It's incremental , So in mid There must be a peak element on the right of , otherwise mid There must be a peak element on the left . By constantly narrowing the scope of search , Finally find the peak element .
When returning the answer , We just need to be in the column where the answer is , Just find the position of the largest element in the column .
class Solution {
public static int[] dx = {
0, 0, 1, -1};
public static int[] dy = {
1, -1, 0, 0};
public static int xLength;
public static int yLength;
public int[] findPeakGrid(int[][] mat) {
xLength = mat.length;
yLength = mat[0].length;
int l = 0, r = yLength - 1;
while (l <= r){
int mid = (l + r) >> 1;
int curMax = -1;
int preMax = -1;
for (int i = 0; i < xLength; ++i){
curMax = Math.max(curMax, mat[i][mid]);
if (mid - 1 >= 0) preMax = Math.max(preMax, mat[i][mid - 1]);
}
if (mid - 1 < 0 || preMax < curMax) l = mid + 1;
else r = mid - 1;
}
int Max = -1, index = -1;
for (int i = 0; i < xLength; ++i){
if (mat[i][r] > Max) {
Max = mat[i][r];
index = i;
}
}
return new int[]{
index ,r};
}
}
1380 The lucky number in the matrix
To give you one m * n Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order .
Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :
The smallest of all elements in the same row
The largest of all elements in the same column
Method
Because each element in the matrix is different , therefore , There is at most one lucky number in a matrix , Prove slightly .
So we just need to find out the only lucky number .
We maintain the minimum value of each row and the maximum value of each column , And then iterate through the entire array , If you find that the minimum value of the row in a certain position is the same as the maximum value of the column , Then the number in this position is the answer .
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int[] lMax = new int[matrix.length];
int[] cMax = new int[matrix[0].length];
for (int i = 0; i < matrix.length; ++i){
lMax[i] = matrix[i][0];
for (int j = 0; j < matrix[0].length; ++j){
lMax[i] = Math.min(lMax[i], matrix[i][j]);
}
}
for (int j = 0; j < matrix[0].length; ++j){
for (int i = 0; i < matrix.length; ++i){
cMax[j] = Math.max(cMax[j], matrix[i][j]);
}
}
for (int i = 0; i < matrix.length; ++i){
for (int j = 0; j < matrix[0].length; ++j){
if (lMax[i] == cMax[j]) {
ArrayList<Integer> res = new ArrayList<>();
res.add(matrix[i][j]);
return res;
}
}
}
return new ArrayList<Integer>();
}
}
边栏推荐
- JS to determine whether there is a value in the object in the array
- Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
- JS modification element attribute flipping commonly used in selenium's Web Automation
- Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
- js创建一个自定义json数组
- Sqli labs customs clearance summary-page3
- Deployment API_ automation_ Problems encountered during test
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- (the 100th blog) written at the end of the second year of doctor's degree -20200818
- Latex compilation error I found no \bibstyle &\bibdata &\citation command
猜你喜欢

如何调试微信内置浏览器应用(企业号、公众号、订阅号)

默认google浏览器打不开链接(点击超链接没有反应)

Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件

Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决

pytest(1) 用例收集规则

Pytest (1) case collection rules

The default Google browser cannot open the link (clicking the hyperlink does not respond)

Fe - wechat applet - Bluetooth ble development research and use

Latex warning: citation "*****" on page y undefined on input line*

Utilisation de la carte et de foreach dans JS
随机推荐
蚂蚁集团g6初探
Solve the problem of bindchange event jitter of swiper component of wechat applet
Detailed definition of tensorrt data format
The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
工具种草福利帖
JS delete the last character of the string
The table component specifies the concatenation parallel method
Latex在VSCODE中编译中文,使用中文路径问题解决
JS modification element attribute flipping commonly used in selenium's Web Automation
Huawei mindspire open source internship machine test questions
selenium的web自动化中常用的js-修改元素属性翻页
浏览器滚动加载更多实现
How to try catch statements that return promise objects in JS
In depth study of JVM bottom layer (IV): class file structure
js的防抖和节流
看完有用的blog
Log - 7 - record a major error in missing documents (A4 paper)
[daily question] - Huawei machine test 01
Fe - use of weex development weex UI components and configuration use
Usage of map and foreach in JS